-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package http | ||
|
||
import "github.com/JulesMike/spoty/json" | ||
|
||
// Error represents a problem details object as described in RFC 7807. | ||
type Error struct { | ||
// A URI reference [RFC3986] that identifies the | ||
// problem type. This specification encourages that, when | ||
// dereferenced, it provide human-readable documentation for the | ||
// problem type (e.g., using HTML [W3C.REC-html5-20141028]). When | ||
// this member is not present, its value is assumed to be | ||
// "about:blank". | ||
Type string `json:"type"` | ||
// A short, human-readable summary of the problem | ||
// type. It SHOULD NOT change from occurrence to occurrence of the | ||
// problem, except for purposes of localization (e.g., using | ||
// proactive content negotiation; see [RFC7231], Section 3.4). | ||
Title string `json:"title"` | ||
// The HTTP status code ([RFC7231], Section 6) | ||
// generated by the origin server for this occurrence of the problem. | ||
Status int `json:"status,omitempty"` | ||
// A human-readable explanation specific to this | ||
// occurrence of the problem. | ||
Detail string `json:"detail,omitempty"` | ||
// A URI reference that identifies the specific | ||
// occurrence of the problem. It may or may not yield further | ||
// information if dereferenced. | ||
Instance string `json:"instance,omitempty"` | ||
// Extensions contains additional data. | ||
Extensions map[string]any `json:"extensions,omitempty"` | ||
} | ||
|
||
// NewError returns a new Error. | ||
func NewError( | ||
errType, | ||
title string, | ||
status int, | ||
detail string, | ||
instance string, | ||
exts map[string]any, | ||
) *Error { | ||
if errType == "" { | ||
errType = "about:blank" | ||
} | ||
|
||
return &Error{ | ||
Type: errType, | ||
Title: title, | ||
Status: status, | ||
Detail: detail, | ||
Instance: instance, | ||
Extensions: exts, | ||
} | ||
} | ||
|
||
func (e *Error) Error() string { | ||
m, _ := json.Marshal(e) //nolint:errcheck | ||
|
||
return string(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters