Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 274c30a
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 S-YOU | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# apierrors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package apierrors | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
var ( | ||
ErrUnknown = New(http.StatusInternalServerError, 0, "unknown") | ||
ErrInternalServerError = New(http.StatusInternalServerError, 1, "internal server error") | ||
ErrBadRequest = New(http.StatusBadRequest, 2, "bad request") | ||
ErrNotFound = New(http.StatusNotFound, 3, "not found") | ||
ErrUnauthorized = New(http.StatusUnauthorized, 4, "unauthorized") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package apierrors | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
func New(status, code int, message string) *Error { | ||
return &Error{ | ||
Code: code, | ||
Status: status, | ||
Message: message, | ||
frame: caller(1), | ||
} | ||
} | ||
|
||
type Error struct { | ||
Code int | ||
Status int | ||
Message string | ||
|
||
err error | ||
base error | ||
frame frame | ||
} | ||
|
||
func (e *Error) Error() string { | ||
return fmt.Sprintf("%s", e.Message) | ||
} | ||
|
||
func (e *Error) Unwrap() error { | ||
return e.err | ||
} | ||
|
||
func (e *Error) Format(s fmt.State, v rune) { | ||
if e.base != nil { | ||
_, _ = fmt.Fprintf(s, "%s: %s\n", e.Error(), e.err) | ||
} | ||
e.frame.Format(s, v) | ||
} | ||
|
||
func (e *Error) Trace() error { | ||
err := *e | ||
err.base = e | ||
err.frame = caller(1) | ||
return &err | ||
} | ||
|
||
func (e *Error) Wrap(next error) error { | ||
err := *e | ||
err.base = e | ||
err.err = next | ||
err.frame = caller(1) | ||
return &err | ||
} | ||
|
||
func (e *Error) Swrap(message string) error { | ||
err := *e | ||
err.base = e | ||
err.err = errors.New(message) | ||
err.frame = caller(1) | ||
return &err | ||
} | ||
|
||
func (e *Error) Swrapf(format string, a ...interface{}) error { | ||
err := *e | ||
err.base = e | ||
err.err = fmt.Errorf(format, a...) | ||
err.frame = caller(1) | ||
return &err | ||
} | ||
|
||
// A frame contains part of a call stack. | ||
type frame struct { | ||
// https://go.googlesource.com/go/+/032678e0fb/src/runtime/extern.go#169 | ||
frames [3]uintptr | ||
} | ||
|
||
// caller returns a frame that describes a frame on the caller's stack. | ||
func caller(skip int) frame { | ||
var s frame | ||
runtime.Callers(skip+1, s.frames[:]) | ||
return s | ||
} | ||
|
||
// location reports the file, line, and function of a frame. | ||
func (f frame) location() (function, file string, line int) { | ||
frames := runtime.CallersFrames(f.frames[:]) | ||
if _, ok := frames.Next(); !ok { | ||
return "", "", 0 | ||
} | ||
fr, ok := frames.Next() | ||
if !ok { | ||
return "", "", 0 | ||
} | ||
return fr.Function, fr.File, fr.Line | ||
} | ||
|
||
// Format prints the stack as error detail. | ||
func (f frame) Format(p fmt.State, v rune) { | ||
function, file, line := f.location() | ||
if function != "" { | ||
_, _ = fmt.Fprintf(p, "%s\n", function) | ||
} | ||
if file != "" { | ||
_, _ = fmt.Fprintf(p, "%s:%d\n", file, line) | ||
} | ||
} |