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

@ -2,6 +2,7 @@ package app
import (
"context"
"errors"
"net/http"
"strconv"
"strings"
@ -14,6 +15,11 @@ import (
"github.com/dhax/go-base/auth/pwdless"
)
// The list of error types returned from account resource.
var (
ErrAccountValidation = errors.New("account validation error")
)
// AccountStore defines database operations for account.
type AccountStore interface {
Get(id int) (*pwdless.Account, error)
@ -103,7 +109,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(err))
render.Render(w, r, ErrValidation(ErrAccountValidation, err.(validation.Errors)))
return
}
render.Render(w, r, ErrRender(err))

View file

@ -1,10 +1,10 @@
package app
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.
@ -33,29 +34,14 @@ func ErrInvalidRequest(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(valErrors error) render.Renderer {
b, _ := json.Marshal(valErrors)
return &ErrValidationResponse{
&ErrResponse{
Err: nil,
HTTPStatusCode: http.StatusUnprocessableEntity,
StatusText: http.StatusText(http.StatusUnprocessableEntity),
ErrorText: "object validation 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,
}
}

View file

@ -2,6 +2,7 @@ package app
import (
"context"
"errors"
"net/http"
"github.com/dhax/go-base/auth/jwt"
@ -11,6 +12,11 @@ import (
validation "github.com/go-ozzo/ozzo-validation"
)
// The list of error types returned from account resource.
var (
ErrProfileValidation = errors.New("profile validation error")
)
// ProfileStore defines database operations for a profile.
type ProfileStore interface {
Get(accountID int) (*models.Profile, error)
@ -85,7 +91,7 @@ func (rs *ProfileResource) update(w http.ResponseWriter, r *http.Request) {
if err := rs.Store.Update(p); err != nil {
switch err.(type) {
case validation.Errors:
render.Render(w, r, ErrValidation(err))
render.Render(w, r, ErrValidation(ErrProfileValidation, err.(validation.Errors)))
return
}
render.Render(w, r, ErrRender(err))