-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
49 lines (43 loc) · 1.37 KB
/
errors.go
File metadata and controls
49 lines (43 loc) · 1.37 KB
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
package errors
import (
"fmt"
"net/http"
)
// Error interface describes errors that contain (HTTP or any other) status code.
type Error interface {
error
Code() int
}
// StatusError is a basic Error interface implementation.
type StatusError struct {
error
code int
}
// NewStatusError is a constructor func for StatusError.
func NewStatusError(code int, cause error) StatusError {
return StatusError{error: cause, code: code}
}
// Code returns HTTP status code (to satisfy Error interface).
func (e StatusError) Code() int {
return e.code
}
// Send provided argument as an HTTP error to the client (retrieving status code
// if available). If status code cannot be retrieved it sends error with status 500
// by default . If value does not implement any of supported error interfaces
// (error/Error) - it tries to convert the value to a string.
func Send(w http.ResponseWriter, err interface{}) {
switch e := err.(type) {
case nil:
// this is not an error (ignore)
case Error:
// retrieve status code and error message
http.Error(w, e.Error(), e.Code())
case error:
// all standard errors without codes will be sent as Internal Server Error,
// error message is not exposed
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
default:
// everything else
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
}
}