...
1 package apperror
2
3 import (
4 "fmt"
5 "runtime"
6 )
7
8
9 var _ JSONResponder = (*JSONError)(nil)
10 var _ Caller = (*JSONError)(nil)
11
12
13 type JSONError struct {
14 code int
15 message string
16 details map[string]interface{}
17 err error
18 source SourceLocation
19 }
20
21 func NewJSONError(err error, code int, message string, details map[string]interface{}) *JSONError {
22 var location SourceLocation
23 if _, file, line, ok := runtime.Caller(1); ok {
24 location = SourceLocation{File: file, Line: line}
25 }
26
27 return &JSONError{
28 code: code,
29 message: message,
30 details: details,
31 err: err,
32 source: location,
33 }
34 }
35
36 func (e JSONError) Error() string {
37 if e.err != nil {
38 return fmt.Sprintf("%s. %v", e.message, e.err.Error())
39 }
40
41 return e.message
42 }
43
44 func (e JSONError) JSONResponse() (int, interface{}) {
45 response := map[string]interface{}{
46 "message": e.message,
47 }
48
49 for k, v := range e.details {
50 response[k] = v
51 }
52
53 return e.code, response
54 }
55
56 func (e JSONError) JSONDetails() map[string]interface{} {
57 return e.details
58 }
59
60 func (e JSONError) Unwrap() error {
61 return e.err
62 }
63
64 func (e JSONError) SourceLocation() SourceLocation {
65 return e.source
66 }
67
View as plain text