pub/internal/domain/error.go

37 lines
632 B
Go
Raw Permalink Normal View History

2020-11-12 19:18:06 +00:00
package domain
import (
"fmt"
"golang.org/x/xerrors"
)
// Error represent a custom error implementation with HTTP status codes support.
//
//nolint:tagliatelle
type Error struct {
Description string `json:"error_description,omitempty"`
Frame xerrors.Frame `json:"-"`
Code int `json:"error"`
}
2023-10-17 08:44:14 +00:00
func (e Error) Error() string {
return fmt.Sprint(e)
2020-11-12 19:18:06 +00:00
}
2023-10-17 08:44:14 +00:00
func (e Error) Format(f fmt.State, r rune) {
xerrors.FormatError(e, f, r)
2020-11-12 19:18:06 +00:00
}
2023-10-17 08:44:14 +00:00
func (e Error) FormatError(p xerrors.Printer) error {
p.Printf("%d: %s", e.Code, e.Description)
2020-11-12 19:18:06 +00:00
if !p.Detail() {
2023-10-17 08:44:14 +00:00
return e
2020-11-12 19:18:06 +00:00
}
2023-10-17 08:44:14 +00:00
e.Frame.Format(p)
2020-11-12 19:18:06 +00:00
return nil
}