all repos — homestead @ c7165476f375b097761e729ac14f9b8a68af98c6

Code for my website

shared/http/error.go (view raw)

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
package http

import (
	"fmt"
	"net/http"
)

type Error interface {
	error
	StatusCode() int
	Message() string
	Unwrap() error
}

type httpError struct {
	code    int
	message string
	cause   error
}

func (e httpError) Error() string {
	if e.message == "" {
		e.message = http.StatusText(e.code)
	}

	return fmt.Sprintf("%d %s", e.code, e.message)
}

func (e httpError) StatusCode() int {
	return e.code
}

func (e httpError) Message() string {
	if e.message == "" {
		e.message = http.StatusText(e.code)
	}

	return e.message
}

func (e httpError) Unwrap() error {
	return e.cause
}

func NewError(message string, code int) httpError {
	return httpError{
		code:    code,
		message: message,
	}
}

func (e httpError) WithCause(cause error) Error {
	return httpError{
		code:    e.code,
		message: e.message,
		cause:   cause,
	}
}