This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomerror.go
118 lines (90 loc) · 2.61 KB
/
customerror.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2021 The customerror Authors. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package customerror
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/go-playground/validator/v10"
)
// CustomError is the base block to create custom errors. It provides context -
// a `Message` to an optional `Err`. Additionally a `Code` - for example "E1010",
// and `StatusCode` can be provided.
type CustomError struct {
// Code can be any custom code, e.g.: E1010.
Code string `json:"code" validate:"omitempty,startswith=E,gte=2"`
// Err optionally wraps the original error.
Err error `json:"-"`
// Human readable message. Minimum length: 3.
Message string `json:"message" validate:"required,gte=3"`
// StatusCode is a valid HTTP status code, e.g.: 404.
StatusCode int `json:"-" validate:"omitempty,gte=100,lte=511"`
}
//////
// Error interface implementation.
//////
// Error interface implementation returns the properly formatted error message.
func (cE *CustomError) Error() string {
errMsg := cE.Message
if cE.Code != "" {
errMsg = fmt.Sprintf("%s: %s", cE.Code, errMsg)
}
if cE.Err != nil {
errMsg = fmt.Errorf("%s. Original Error: %w", errMsg, cE.Err).Error()
}
return errMsg
}
// APIError is like error plus status code information.
func (cE *CustomError) APIError() string {
errMsg := cE.Message
if cE.Code != "" {
errMsg = fmt.Sprintf("%s: %s", cE.Code, errMsg)
}
if cE.StatusCode != 0 {
errMsg = fmt.Sprintf("%s (%d - %s)", errMsg, cE.StatusCode, http.StatusText(cE.StatusCode))
}
if cE.Err != nil {
errMsg = fmt.Errorf("%s. Original Error: %w", errMsg, cE.Err).Error()
}
return errMsg
}
// Unwrap interface implementation returns inner error.
func (cE *CustomError) Unwrap() error {
return cE.Err
}
// Is interface implementation ensures chain continuity. Treats `CustomError` as
// equivalent to `err`.
//
//nolint:errorlint
func (cE *CustomError) Is(err error) bool {
return cE.Err == err
}
// Wrap `customError` around `errors`.
func Wrap(customError error, errors ...error) error {
errMsgs := []string{}
for _, err := range errors {
if err != nil {
errMsgs = append(errMsgs, err.Error())
}
}
return fmt.Errorf("%w. Wrapped Error(s): %s", customError, strings.Join(errMsgs, ". "))
}
//////
// Factory.
//////
// New is the custom error factory.
func New(message string, opts ...Option) error {
cE := &CustomError{
Message: message,
}
for _, opt := range opts {
opt(cE)
}
if err := validator.New().Struct(cE); err != nil {
log.Fatalf("Invalid custom error. %s\n", err)
return nil
}
return cE
}