-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patherrors.go.tmpl
More file actions
88 lines (77 loc) · 2.68 KB
/
errors.go.tmpl
File metadata and controls
88 lines (77 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
{{- define "errors" -}}
{{- $webrpcErrors := .WebrpcErrors -}}
{{- $schemaErrors := .SchemaErrors -}}
//
// Errors
//
type WebrpcErrorParams = { name?: string, code?: number, message?: string, status?: number, cause?: string }
export class WebrpcError extends Error {
code: number
status: number
constructor(error: WebrpcErrorParams = {}) {
super(error.message)
this.name = error.name || 'WebrpcEndpointError'
this.code = typeof error.code === 'number' ? error.code : 0
this.message = error.message || `endpoint error`
this.status = typeof error.status === 'number' ? error.status : 400
if (error.cause !== undefined) this.cause = error.cause
Object.setPrototypeOf(this, WebrpcError.prototype)
}
static new(payload: any): WebrpcError {
return new this({ message: payload.message, code: payload.code, status: payload.status, cause: payload.cause })
}
}
{{ range $_, $error := $webrpcErrors}}
export class {{$error.Name}}Error extends WebrpcError {
constructor(error: WebrpcErrorParams = {}) {
super(error)
this.name = error.name || '{{$error.Name}}'
this.code = typeof error.code === 'number' ? error.code : {{$error.Code}}
this.message = error.message || `{{$error.Message}}`
this.status = typeof error.status === 'number' ? error.status : {{$error.HTTPStatus}}
if (error.cause !== undefined) this.cause = error.cause
Object.setPrototypeOf(this, {{$error.Name}}Error.prototype)
}
}
{{ end }}
//
// Schema errors
//
{{ range $_, $error := $schemaErrors}}
export class {{$error.Name}}Error extends WebrpcError {
constructor(error: WebrpcErrorParams = {}) {
super(error)
this.name = error.name || '{{$error.Name}}'
this.code = typeof error.code === 'number' ? error.code : {{$error.Code}}
this.message = error.message || `{{$error.Message}}`
this.status = typeof error.status === 'number' ? error.status : {{$error.HTTPStatus}}
if (error.cause !== undefined) this.cause = error.cause
Object.setPrototypeOf(this, {{$error.Name}}Error.prototype)
}
}
{{ end }}
export enum errors {
{{- range $_, $error := $webrpcErrors}}
{{$error.Name}} = '{{$error.Name}}',
{{- end}}
{{- range $_, $error := $schemaErrors}}
{{$error.Name}} = '{{$error.Name}}',
{{- end}}
}
export enum WebrpcErrorCodes {
{{- range $_, $error := $webrpcErrors}}
{{$error.Name}} = {{$error.Code}},
{{- end }}
{{- range $_, $error := $schemaErrors}}
{{$error.Name}} = {{$error.Code}},
{{- end }}
}
export const webrpcErrorByCode: { [code: number]: any } = {
{{- range $_, $error := $webrpcErrors}}
[{{$error.Code}}]: {{$error.Name}}Error,
{{- end }}
{{- range $_, $error := $schemaErrors}}
[{{$error.Code}}]: {{$error.Name}}Error,
{{- end }}
}
{{end}}