-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathexamples_test.go
More file actions
93 lines (74 loc) · 2.61 KB
/
examples_test.go
File metadata and controls
93 lines (74 loc) · 2.61 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
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
90
91
92
93
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package errors_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"github.com/go-openapi/errors"
)
func ExampleNew() {
// Create a generic API error with custom code
err := errors.New(400, "invalid input: %s", "email")
fmt.Printf("error: %v\n", err)
fmt.Printf("code: %d\n", err.Code())
// Create common HTTP errors
notFound := errors.NotFound("user %s not found", "john-doe")
fmt.Printf("not found: %v\n", notFound)
fmt.Printf("not found code: %d\n", notFound.Code())
notImpl := errors.NotImplemented("feature: dark mode")
fmt.Printf("not implemented: %v\n", notImpl)
// Output:
// error: invalid input: email
// code: 400
// not found: user john-doe not found
// not found code: 404
// not implemented: feature: dark mode
}
func ExampleServeError() {
// Create a simple validation error
err := errors.Required("email", "body", nil)
// Simulate HTTP response
recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodPost, "/api/users", nil)
// Serve the error as JSON
errors.ServeError(recorder, request, err)
fmt.Printf("status: %d\n", recorder.Code)
fmt.Printf("content-type: %s\n", recorder.Header().Get("Content-Type"))
// Parse and display the JSON response
var response map[string]any
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err == nil {
fmt.Printf("error code: %.0f\n", response["code"])
fmt.Printf("error message: %s\n", response["message"])
}
// Output:
// status: 422
// content-type: application/json
// error code: 602
// error message: email in body is required
}
func ExampleCompositeValidationError() {
errs := make([]error, 0, 3)
// Collect multiple validation errors
errs = append(errs, errors.Required("name", "body", nil))
errs = append(errs, errors.TooShort("description", "body", 10, "short"))
errs = append(errs, errors.InvalidType("age", "body", "integer", "abc"))
// Combine them into a composite error
compositeErr := errors.CompositeValidationError(errs...)
fmt.Printf("error count: %d\n", len(errs))
fmt.Printf("composite error: %v\n", compositeErr)
fmt.Printf("code: %d\n", compositeErr.Code())
// Can unwrap to access individual errors
if unwrapped := compositeErr.Unwrap(); unwrapped != nil {
fmt.Printf("unwrapped count: %d\n", len(unwrapped))
}
// Output:
// error count: 3
// composite error: validation failure list:
// name in body is required
// description in body should be at least 10 chars long
// age in body must be of type integer: "abc"
// code: 422
// unwrapped count: 3
}