-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
38 lines (31 loc) · 922 Bytes
/
error.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
package main
import (
"fmt"
"net/http"
"runtime"
)
type HTTPError struct {
StatusCode int
Message string
}
var (
ForbiddenError = NewHTTPError(http.StatusForbidden, "whoops... Your permissions are insufficient")
ReloadPageError = NewHTTPError(http.StatusBadRequest, "whoops... Something went wrong. Please reload this page or try again")
TryAgainLaterError = NewHTTPError(http.StatusInternalServerError, "whoops... Something went wrong. Please try again later")
)
func (e HTTPError) Error() string {
return e.Message
}
func NewHTTPError(status int, message string) HTTPError {
return HTTPError{StatusCode: status, Message: message}
}
func WrapErrorWithTrace(err error) error {
return WrapErrorWithTraceSkip(err, 2)
}
func WrapErrorWithTraceSkip(err error, skip int) error {
_, file, line, ok := runtime.Caller(skip)
if !ok {
return err
}
return fmt.Errorf("%s:%d: %w", file, line, err)
}