♻️ Improved custom error creation
This commit is contained in:
parent
85cfafdf21
commit
b60aab7be5
|
@ -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) {
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 (
|
||||
|
|
Loading…
Reference in New Issue