♻️ Improved custom error creation

This commit is contained in:
Maxim Lebedev 2022-01-30 00:30:57 +05:00
parent 85cfafdf21
commit b60aab7be5
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
6 changed files with 18 additions and 9 deletions

View File

@ -25,7 +25,7 @@ var (
ActionTicket = Action{uid: "ticket"} ActionTicket = Action{uid: "ticket"}
) )
var ErrActionUnknown error = NewError(ErrorCodeInvalidRequest, "unknown action method") var ErrActionUnknown error = NewError(ErrorCodeInvalidRequest, "unknown action method", "")
// ParseAction parse string identifier of action into struct enum. // ParseAction parse string identifier of action into struct enum.
func ParseAction(uid string) (Action, error) { func ParseAction(uid string) (Action, error) {

View File

@ -54,7 +54,11 @@ var (
} }
) )
var ErrCodeChallengeMethodUnknown error = NewError(ErrorCodeInvalidRequest, "unknown code_challene_method") var ErrCodeChallengeMethodUnknown error = NewError(
ErrorCodeInvalidRequest,
"unknown code_challene_method",
"https://indieauth.net/source/#authorization-request",
)
//nolint: gochecknoglobals // NOTE(toby3d): maps cannot be constants //nolint: gochecknoglobals // NOTE(toby3d): maps cannot be constants
var slugsMethods = map[string]CodeChallengeMethod{ var slugsMethods = map[string]CodeChallengeMethod{

View File

@ -11,7 +11,7 @@ type Email struct {
host string host string
} }
var ErrEmailInvalid error = NewError(ErrorCodeInvalidRequest, "cannot parse email") var ErrEmailInvalid error = NewError(ErrorCodeInvalidRequest, "cannot parse email", "")
// ParseEmail parse strings to email identifier. // ParseEmail parse strings to email identifier.
func ParseEmail(src string) (*Email, error) { func ParseEmail(src string) (*Email, error) {

View File

@ -171,20 +171,25 @@ func (e Error) SetReirectURI(u *http.URI) {
} }
// NewError creates a new Error with the stack pointing to the function call // NewError creates a new Error with the stack pointing to the function call
// line. // line number.
// //
// If no code or ErrorCodeUndefined is provided, ErrorCodeAccessDenied will be // If no code or ErrorCodeUndefined is provided, ErrorCodeAccessDenied will be
// used instead. // used instead.
func NewError(code ErrorCode, description string) *Error { func NewError(code ErrorCode, description, uri string, requestState ...string) *Error {
if code == ErrorCodeUndefined { if code == ErrorCodeUndefined {
code = ErrorCodeAccessDenied code = ErrorCodeAccessDenied
} }
var state string
if len(requestState) > 0 {
state = requestState[0]
}
return &Error{ return &Error{
Code: code, Code: code,
Description: description, Description: description,
URI: "", URI: uri,
State: "", State: state,
frame: xerrors.Caller(1), frame: xerrors.Caller(1),
} }
} }

View File

@ -7,6 +7,6 @@ import (
) )
func ExampleNewError() { func ExampleNewError() {
fmt.Printf("%v", domain.NewError(domain.ErrorCodeInvalidRequest, "client_id MUST be provided")) fmt.Printf("%v", domain.NewError(domain.ErrorCodeInvalidRequest, "client_id MUST be provided", ""))
// Output: invalid_request: client_id MUST be provided // Output: invalid_request: client_id MUST be provided
} }

View File

@ -20,7 +20,7 @@ type (
Scopes []Scope Scopes []Scope
) )
var ErrScopeUnknown error = NewError(ErrorCodeInvalidRequest, "unknown scope") var ErrScopeUnknown error = NewError(ErrorCodeInvalidRequest, "unknown scope", "https://indieweb.org/scope")
//nolint: gochecknoglobals // NOTE(toby3d): structs cannot be constants //nolint: gochecknoglobals // NOTE(toby3d): structs cannot be constants
var ( var (