|
| 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 | +} |
0 commit comments