-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_error_test.go
38 lines (32 loc) · 1.09 KB
/
http_error_test.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
package mux_test
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/danikarik/mux"
)
func ExampleHTTPError() {
err := mux.NewHTTPError(http.StatusBadRequest, "Bad Request")
data, _ := json.Marshal(err)
fmt.Println(string(data))
opts := []func(e *mux.HTTPError){
func(e *mux.HTTPError) { e.ShowError = true },
func(e *mux.HTTPError) { e.InternalError = errors.New("Unexpected Error") },
func(e *mux.HTTPError) { e.InternalMessage = "Failed" },
func(e *mux.HTTPError) { e.ErrorID = "123" },
}
err = mux.NewHTTPError(http.StatusInternalServerError, "Server Error", opts...)
data, _ = json.Marshal(err)
fmt.Println(string(data))
err = mux.NewHTTPError(http.StatusUnauthorized, "Unauthorized").
WithInternalMessage("Failed").
WithErrorID("456").
WithShowError(true)
data, _ = json.Marshal(err)
fmt.Println(string(data))
// Output:
// {"code":400,"message":"Bad Request"}
// {"code":500,"message":"Server Error","internalError":"Unexpected Error","internalMessage":"Failed","id":"123"}
// {"code":401,"message":"Unauthorized","internalMessage":"Failed","id":"456"}
}