From f089dd0901b092e05a2ea5c289236c2a3619f74c Mon Sep 17 00:00:00 2001 From: Reza Date: Mon, 27 May 2024 11:47:38 +0200 Subject: [PATCH] errors --- errors/context.go | 66 -------------------------------------------- evo.context.go | 23 +++++++++++++++ lib/errors/errors.go | 64 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 66 deletions(-) delete mode 100644 errors/context.go create mode 100644 lib/errors/errors.go diff --git a/errors/context.go b/errors/context.go deleted file mode 100644 index a3b3a456..00000000 --- a/errors/context.go +++ /dev/null @@ -1,66 +0,0 @@ -package e - -import "fmt" - -type Error struct { - Type string `json:"type"` - Field string `json:"field,omitempty"` - Message string `json:"message"` - Solution string `json:"solution,omitempty"` - Params *[]any `json:"params,omitempty"` -} - -type Errors []Error - -func New(t, field, message, solution string, params ...any) *Error { - return &Error{ - t, field, message, solution, ¶ms, - } -} - -func Field(field, message any, params ...any) *Error { - return New("field", fmt.Sprint(field), fmt.Sprint(message), "", params) -} - -func Context(message any, params ...any) *Error { - return New("context", "", fmt.Sprint(message), "", params) -} - -func (e *Error) SetSolution(s string) *Error { - e.Solution = s - return e -} - -func (e *Error) SetParams(params ...any) *Error { - e.Params = ¶ms - return e -} - -func (e *Error) SetType(t string) *Error { - e.Type = t - return e -} - -func (e *Error) SetMessage(message string) *Error { - e.Message = message - return e -} - -func (e *Error) SetFiled(field string) *Error { - e.Field = field - return e -} - -func (e *Errors) Push(error *Error) *Errors { - *e = append(*e, *error) - return e -} - -func (e *Errors) Exist() bool { - return len(*e) != 0 -} - -func (e *Errors) Clear() *Errors { - e = &Errors{} - return e -} diff --git a/evo.context.go b/evo.context.go index d81498c2..84e4fc96 100644 --- a/evo.context.go +++ b/evo.context.go @@ -2,6 +2,7 @@ package evo import ( "fmt" + "github.com/getevo/evo/v2/lib/errors" "net/url" "reflect" "strings" @@ -135,7 +136,29 @@ func (r *Request) WriteResponse(resp ...any) { r._writeResponse(r.Response) return } + if v, ok := instance.(errors.HTTPError); ok { + if v.StatusCode > 0 { + r.Status(v.StatusCode) + } + if len(v.Cookies) > 0 { + for idx := range v.Cookies { + r.SetRawCookie(v.Cookies[idx]) + } + } + if v.ContentType != "" { + r.SetHeader("Content-Type", v.ContentType) + } else { + r.SetHeader("Content-Type", fiber.MIMEApplicationJSONCharsetUTF8) + } + if len(v.Headers) > 0 { + for header, value := range v.Headers { + r.SetHeader(header, value) + } + } + r.Write(v.Data) + return + } if v, ok := instance.(outcome.Response); ok { if v.StatusCode > 0 { diff --git a/lib/errors/errors.go b/lib/errors/errors.go new file mode 100644 index 00000000..54f019fc --- /dev/null +++ b/lib/errors/errors.go @@ -0,0 +1,64 @@ +package errors + +import ( + "fmt" + "github.com/getevo/evo/v2/lib/outcome" + "github.com/getevo/evo/v2/lib/text" +) + +type HTTPError outcome.Response + +var ( + NotFound = New("Not Found", 404) + BadRequest = New("Bad Request", 400) + Unauthorized = New("Unauthorized", 401) + Forbidden = New("Forbidden", 403) + Internal = New("Internal Server Error", 500) + NotAcceptable = New("Not Acceptable", 406) + Conflict = New("Conflict", 409) + Precondition = New("Precondition Failed", 412) + UnsupportedMedia = New("Unsupported Media Type", 415) + Gone = New("Gone", 410) + RequestTimeout = New("Request Timeout", 408) + RequestEntityTooLarge = New("Request Entity Too Large", 413) + RequestURITooLong = New("Request URI Too Long", 414) + RequestHeaderFieldsTooLarge = New("Request Header Fields Too Large", 431) + UnavailableForLegalReasons = New("Unavailable For Legal Reasons", 451) + PayloadTooLarge = New("Payload Too Large", 413) + TooManyRequests = New("Too Many Requests", 429) +) + +func New(err ...interface{}) HTTPError { + var r = Response{ + Success: false, + Error: "Internal Server Error", + } + var out = HTTPError{ + StatusCode: 500, + } + + for i, _ := range err { + switch v := err[i].(type) { + case string: + r.Error = v + case error: + r.Error = v.Error() + case int: + out.StatusCode = v + default: + r.Error = fmt.Sprint(v) + } + } + out.Data = text.ToJSON(r) + return out +} + +func (e HTTPError) Code(code int) HTTPError { + e.StatusCode = code + return e +} + +type Response struct { + Success bool `json:"success"` + Error string `json:"error,omitempty"` +}