This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy patherror.go
89 lines (73 loc) · 1.91 KB
/
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
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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package clevergo
import (
"errors"
"fmt"
"net/http"
)
// Error defines an HTTP response error.
type Error interface {
error
Status() int
}
// Errors
var (
ErrNotFound = StatusError{http.StatusNotFound, errors.New(http.StatusText(http.StatusNotFound))}
ErrMethodNotAllowed = StatusError{http.StatusMethodNotAllowed, errors.New(http.StatusText(http.StatusMethodNotAllowed))}
)
type errorHandler struct {
}
func (h *errorHandler) middleware(next Handle) Handle {
return func(c *Context) (err error) {
if err := next(c); err != nil {
h.handleError(c, err)
}
return nil
}
}
func (h *errorHandler) handleError(c *Context, err error) {
c.Logger().Errorf("clevergo: error handler catches an error: %s", err.Error())
switch e := err.(type) {
case Error:
c.Error(e.Status(), err.Error())
default:
c.Error(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
}
// ErrorHandler returns a error handler middleware.
func ErrorHandler() MiddlewareFunc {
h := &errorHandler{}
return h.middleware
}
// StatusError implements Error interface.
type StatusError struct {
Code int
Err error
}
// NewError returns a status error with the given code and error.
func NewError(code int, err error) StatusError {
return StatusError{code, err}
}
// Error implements error.Error.
func (e StatusError) Error() string {
return e.Err.Error()
}
// Status implements Error.Status.
func (e StatusError) Status() int {
return e.Code
}
// PanicError is an error that contains panic information.
type PanicError struct {
// Context.
Context *Context
// Recovery data.
Data interface{}
// Debug stack.
Stack []byte
}
// Error implements error interface.
func (e PanicError) Error() string {
return fmt.Sprintf("Panic: %v\n%s\n", e.Data, e.Stack)
}