♻️ 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"}
)
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.
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
var slugsMethods = map[string]CodeChallengeMethod{

View File

@ -11,7 +11,7 @@ type Email struct {
host string
}
var ErrEmailInvalid error = NewError(ErrorCodeInvalidRequest, "cannot parse email")
var ErrEmailInvalid error = NewError(ErrorCodeInvalidRequest, "cannot parse email", "")
// ParseEmail parse strings to email identifier.
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
// line.
// line number.
//
// If no code or ErrorCodeUndefined is provided, ErrorCodeAccessDenied will be
// used instead.
func NewError(code ErrorCode, description string) *Error {
func NewError(code ErrorCode, description, uri string, requestState ...string) *Error {
if code == ErrorCodeUndefined {
code = ErrorCodeAccessDenied
}
var state string
if len(requestState) > 0 {
state = requestState[0]
}
return &Error{
Code: code,
Description: description,
URI: "",
State: "",
URI: uri,
State: state,
frame: xerrors.Caller(1),
}
}

View File

@ -7,6 +7,6 @@ import (
)
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
}

View File

@ -20,7 +20,7 @@ type (
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
var (