package apperror import ( "fmt" "runtime" ) // Assert that JSONError implements interfaces. var _ JSONResponder = (*JSONError)(nil) var _ Caller = (*JSONError)(nil) // JSONError represents an AbortWithStatusJSON error type JSONError struct { code int message string details map[string]interface{} err error source SourceLocation } func NewJSONError(err error, code int, message string, details map[string]interface{}) *JSONError { var location SourceLocation if _, file, line, ok := runtime.Caller(1); ok { location = SourceLocation{File: file, Line: line} } return &JSONError{ code: code, message: message, details: details, err: err, source: location, } } func (e JSONError) Error() string { if e.err != nil { return fmt.Sprintf("%s. %v", e.message, e.err.Error()) } return e.message } func (e JSONError) JSONResponse() (int, interface{}) { response := map[string]interface{}{ "message": e.message, } for k, v := range e.details { response[k] = v } return e.code, response } func (e JSONError) JSONDetails() map[string]interface{} { return e.details } func (e JSONError) Unwrap() error { return e.err } func (e JSONError) SourceLocation() SourceLocation { return e.source }