Skip to content

Commit

Permalink
🐛error pointer receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
bouroo committed Jan 1, 2023
1 parent afe9aa2 commit ee8b633
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
28 changes: 28 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package helpers

import "net/http"

// Error represents an error that occurred while handling a request.
type Error struct {
Code int `json:"code"`
Source interface{} `json:"source,omitempty"`
Title string `json:"title,omitempty"`
Messages []string `json:"messages"`
}

func (e *Error) Error() (errStr string) {
if len(e.Messages) != 0 {
errStr = e.Messages[0]
}
return
}

func NewError(code int, source string, message ...string) (err *Error) {
err = &Error{
Code: code,
Source: source,
Title: http.StatusText(code),
Messages: message,
}
return
}
22 changes: 5 additions & 17 deletions model.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
package helpers

type ResponseForm struct {
Success bool `json:"success"`
Result interface{} `json:"result,omitempty"`
Messages []string `json:"messages,omitempty"`
Errors []Error `json:"errors,omitempty"`
ResultInfo *ResultInfo `json:"result_info,omitempty"`
Success bool `json:"success"`
Result interface{} `json:"result,omitempty"`
Messages []string `json:"messages,omitempty"`
Errors []ResponseError `json:"errors,omitempty"`
ResultInfo *ResultInfo `json:"result_info,omitempty"`
}

// backward complatible
type ResponseError struct {
*Error
}

// Error represents an error that occurred while handling a request.
type Error struct {
Code int `json:"code"`
Source interface{} `json:"source,omitempty"`
Title string `json:"title,omitempty"`
Message string `json:"message"`
}

func (e *Error) Error() string {
return e.Message
}

type ResultInfo struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Expand Down
9 changes: 0 additions & 9 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,3 @@ func ExtractAuthString(authStr string) (auth HttpAuth, err error) {
}
return
}

func NewError(code int, source string, title string, message string) Error {
return Error{
Code: code,
Source: source,
Title: title,
Message: message,
}
}

0 comments on commit ee8b633

Please sign in to comment.