Support for HTTP errors, add sink impl.
This commit is contained in:
@@ -9,6 +9,9 @@ import (
|
||||
"strings"
|
||||
"errors"
|
||||
"time"
|
||||
{{- if $.SupportHttpErrors }}
|
||||
"net/http"
|
||||
{{- end }}
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/actionlog"
|
||||
{{- range $import := $.Import }}
|
||||
@@ -51,6 +54,10 @@ type (
|
||||
wrap error
|
||||
|
||||
props *{{ $.Service }}ActionProps
|
||||
|
||||
{{ if $.SupportHttpErrors }}
|
||||
httpStatusCode int
|
||||
{{ end }}
|
||||
}
|
||||
{{ end }}
|
||||
)
|
||||
@@ -255,6 +262,18 @@ func (e *{{ $.Service }}Error) LoggableAction() *actionlog.Action {
|
||||
Meta: e.props.serialize(),
|
||||
}
|
||||
}
|
||||
|
||||
{{ if $.SupportHttpErrors }}
|
||||
func (e *{{ $.Service }}Error) HttpResponse(w http.ResponseWriter) {
|
||||
var code = e.httpStatusCode
|
||||
if code == 0 {
|
||||
code = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
http.Error(w, e.message, code)
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{ end }}
|
||||
|
||||
{{ if $.Actions }}
|
||||
@@ -313,6 +332,10 @@ func {{ camelCase "" $.Service "Err" $e.Error }}(props ... *{{ $.Service }}Actio
|
||||
log: "{{ $e.Log }}",
|
||||
severity: {{ $e.SeverityConstName }},
|
||||
props: func() *{{ $.Service }}ActionProps { if len(props) > 0 { return props[0] }; return nil}(),
|
||||
|
||||
{{ if $e.HttpStatus }}
|
||||
httpStatusCode: http.{{ $e.HttpStatus }},
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
if len(props) > 0 {
|
||||
|
||||
@@ -65,6 +65,9 @@ type (
|
||||
|
||||
// Error severity
|
||||
Severity string `yaml:"severity"`
|
||||
|
||||
// HTTP Status code for this error
|
||||
HttpStatus string `yaml:"httpStatus"`
|
||||
}
|
||||
)
|
||||
|
||||
@@ -132,6 +135,10 @@ func procDef(path, output string) {
|
||||
// Default severity for errors
|
||||
DefaultErrorSeverity string `yaml:"defaultErrorSeverity"`
|
||||
|
||||
// If at least one of the errors has HTTP status defined,
|
||||
// add support for http errors
|
||||
SupportHttpErrors bool
|
||||
|
||||
Props []*propsDef
|
||||
Actions []*actionDef
|
||||
Errors []*errorDef
|
||||
@@ -209,6 +216,10 @@ func procDef(path, output string) {
|
||||
if e.Severity == "" {
|
||||
e.Severity = tplData.DefaultErrorSeverity
|
||||
}
|
||||
|
||||
if e.HttpStatus != "" {
|
||||
tplData.SupportHttpErrors = true
|
||||
}
|
||||
}
|
||||
|
||||
checkHandle := func(s string) {
|
||||
|
||||
Reference in New Issue
Block a user