Skip to content

Commit 80178b1

Browse files
committed
doc: increased godoc coverage, added testable examples
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 7975566 commit 80178b1

7 files changed

Lines changed: 109 additions & 5 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
secrets.yml
2-
coverage.out
2+
*.out
3+
settings.local.json

api.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ type apiError struct {
2828
message string
2929
}
3030

31+
// Error implements the standard error interface.
3132
func (a *apiError) Error() string {
3233
return a.message
3334
}
3435

36+
// Code returns the HTTP status code associated with this error.
3537
func (a *apiError) Code() int32 {
3638
return a.code
3739
}
@@ -78,11 +80,12 @@ type MethodNotAllowedError struct {
7880
message string
7981
}
8082

83+
// Error implements the standard error interface.
8184
func (m *MethodNotAllowedError) Error() string {
8285
return m.message
8386
}
8487

85-
// Code the error code.
88+
// Code returns 405 (Method Not Allowed) as the HTTP status code.
8689
func (m *MethodNotAllowedError) Code() int32 {
8790
return m.code
8891
}

examples_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package errors_test
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
"net/http"
10+
"net/http/httptest"
11+
12+
"github.com/go-openapi/errors"
13+
)
14+
15+
func ExampleNew() {
16+
// Create a generic API error with custom code
17+
err := errors.New(400, "invalid input: %s", "email")
18+
fmt.Printf("error: %v\n", err)
19+
fmt.Printf("code: %d\n", err.Code())
20+
21+
// Create common HTTP errors
22+
notFound := errors.NotFound("user %s not found", "john-doe")
23+
fmt.Printf("not found: %v\n", notFound)
24+
fmt.Printf("not found code: %d\n", notFound.Code())
25+
26+
notImpl := errors.NotImplemented("feature: dark mode")
27+
fmt.Printf("not implemented: %v\n", notImpl)
28+
29+
// Output:
30+
// error: invalid input: email
31+
// code: 400
32+
// not found: user john-doe not found
33+
// not found code: 404
34+
// not implemented: feature: dark mode
35+
}
36+
37+
func ExampleServeError() {
38+
// Create a simple validation error
39+
err := errors.Required("email", "body", nil)
40+
41+
// Simulate HTTP response
42+
recorder := httptest.NewRecorder()
43+
request := httptest.NewRequest(http.MethodPost, "/api/users", nil)
44+
45+
// Serve the error as JSON
46+
errors.ServeError(recorder, request, err)
47+
48+
fmt.Printf("status: %d\n", recorder.Code)
49+
fmt.Printf("content-type: %s\n", recorder.Header().Get("Content-Type"))
50+
51+
// Parse and display the JSON response
52+
var response map[string]any
53+
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err == nil {
54+
fmt.Printf("error code: %.0f\n", response["code"])
55+
fmt.Printf("error message: %s\n", response["message"])
56+
}
57+
58+
// Output:
59+
// status: 422
60+
// content-type: application/json
61+
// error code: 602
62+
// error message: email in body is required
63+
}
64+
65+
func ExampleCompositeValidationError() {
66+
var errs []error
67+
68+
// Collect multiple validation errors
69+
errs = append(errs, errors.Required("name", "body", nil))
70+
errs = append(errs, errors.TooShort("description", "body", 10, "short"))
71+
errs = append(errs, errors.InvalidType("age", "body", "integer", "abc"))
72+
73+
// Combine them into a composite error
74+
compositeErr := errors.CompositeValidationError(errs...)
75+
76+
fmt.Printf("error count: %d\n", len(errs))
77+
fmt.Printf("composite error: %v\n", compositeErr)
78+
fmt.Printf("code: %d\n", compositeErr.Code())
79+
80+
// Can unwrap to access individual errors
81+
if unwrapped := compositeErr.Unwrap(); unwrapped != nil {
82+
fmt.Printf("unwrapped count: %d\n", len(unwrapped))
83+
}
84+
85+
// Output:
86+
// error count: 3
87+
// composite error: validation failure list:
88+
// name in body is required
89+
// description in body should be at least 10 chars long
90+
// age in body must be of type integer: "abc"
91+
// code: 422
92+
// unwrapped count: 3
93+
}

headers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ type Validation struct { //nolint: errname // changing the name to abide by the
1919
Values []any
2020
}
2121

22+
// Error implements the standard error interface.
2223
func (e *Validation) Error() string {
2324
return e.message
2425
}
2526

26-
// Code the error code.
27+
// Code returns the HTTP status code for this validation error.
28+
// Returns 422 (Unprocessable Entity) by default.
2729
func (e *Validation) Code() int32 {
2830
return e.code
2931
}

middleware.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type APIVerificationFailed struct { //nolint: errname
1717
MissingRegistration []string `json:"missingRegistration,omitempty"`
1818
}
1919

20+
// Error implements the standard error interface.
2021
func (v *APIVerificationFailed) Error() string {
2122
buf := bytes.NewBuffer(nil)
2223

parsing.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ func NewParseError(name, in, value string, reason error) *ParseError {
3737
}
3838
}
3939

40+
// Error implements the standard error interface.
4041
func (e *ParseError) Error() string {
4142
return e.message
4243
}
4344

44-
// Code returns the http status code for this error.
45+
// Code returns 400 (Bad Request) as the HTTP status code for parsing errors.
4546
func (e *ParseError) Code() int32 {
4647
return e.code
4748
}

schema.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const (
7171

7272
// InvalidTypeCode is used for any subclass of invalid types.
7373
InvalidTypeCode = maximumValidHTTPCode + iota
74+
// RequiredFailCode indicates a required field is missing.
7475
RequiredFailCode
7576
TooLongFailCode
7677
TooShortFailCode
@@ -98,11 +99,12 @@ type CompositeError struct {
9899
message string
99100
}
100101

101-
// Code for this error.
102+
// Code returns the HTTP status code for this composite error.
102103
func (c *CompositeError) Code() int32 {
103104
return c.code
104105
}
105106

107+
// Error implements the standard error interface.
106108
func (c *CompositeError) Error() string {
107109
if len(c.Errors) > 0 {
108110
msgs := []string{c.message + ":"}
@@ -117,6 +119,7 @@ func (c *CompositeError) Error() string {
117119
return c.message
118120
}
119121

122+
// Unwrap implements the [errors.Unwrap] interface.
120123
func (c *CompositeError) Unwrap() []error {
121124
return c.Errors
122125
}

0 commit comments

Comments
 (0)