use json.Marshal provided with validation

This commit is contained in:
dhax 2019-01-08 23:07:17 +01:00
parent cd7d44fcd8
commit 2d7f6be830
5 changed files with 40 additions and 56 deletions

View file

@ -120,7 +120,7 @@ func (rs *AccountResource) create(w http.ResponseWriter, r *http.Request) {
if err := rs.Store.Create(data.Account); err != nil {
switch err.(type) {
case validation.Errors:
render.Render(w, r, ErrValidation(ErrAccountValidation, err))
render.Render(w, r, ErrValidation(ErrAccountValidation, err.(validation.Errors)))
return
}
render.Render(w, r, ErrInvalidRequest(err))
@ -145,7 +145,7 @@ func (rs *AccountResource) update(w http.ResponseWriter, r *http.Request) {
if err := rs.Store.Update(acc); err != nil {
switch err.(type) {
case validation.Errors:
render.Render(w, r, ErrValidation(ErrAccountValidation, err))
render.Render(w, r, ErrValidation(ErrAccountValidation, err.(validation.Errors)))
return
}
render.Render(w, r, ErrInvalidRequest(err))

View file

@ -1,10 +1,10 @@
package admin
import (
"encoding/json"
"net/http"
"github.com/go-chi/render"
validation "github.com/go-ozzo/ozzo-validation"
)
// ErrResponse renderer type for handling all sorts of errors.
@ -12,9 +12,10 @@ type ErrResponse struct {
Err error `json:"-"` // low-level runtime error
HTTPStatusCode int `json:"-"` // http response status code
StatusText string `json:"status"` // user-level status message
AppCode int64 `json:"code,omitempty"` // application-specific error code
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
StatusText string `json:"status"` // user-level status message
AppCode int64 `json:"code,omitempty"` // application-specific error code
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
ValidationErrors validation.Errors `json:"errors,omitempty"` // user level model validation errors
}
// Render sets the application-specific error code in AppCode.
@ -43,29 +44,14 @@ func ErrRender(err error) render.Renderer {
}
}
// ErrValidationResponse renderer for handling validation errors.
type ErrValidationResponse struct {
*ErrResponse
Errors string `json:"errors,omitempty"`
}
// Render sets the application-specific error code in AppCode.
func (ev *ErrValidationResponse) Render(w http.ResponseWriter, r *http.Request) error {
render.Status(r, ev.ErrResponse.HTTPStatusCode)
return nil
}
// ErrValidation returns status 422 Unprocessable Entity stating validation errors.
func ErrValidation(err error, valErrors error) render.Renderer {
b, _ := json.Marshal(valErrors)
return &ErrValidationResponse{
&ErrResponse{
Err: err,
HTTPStatusCode: http.StatusUnprocessableEntity,
StatusText: http.StatusText(http.StatusUnprocessableEntity),
ErrorText: err.Error(),
},
string(b),
func ErrValidation(err error, valErr validation.Errors) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: http.StatusUnprocessableEntity,
StatusText: http.StatusText(http.StatusUnprocessableEntity),
ErrorText: err.Error(),
ValidationErrors: valErr,
}
}