| 3384 | } |
| 3385 | |
| 3386 | func TestCustomError(t *testing.T) { |
| 3387 | orig := huma.NewError |
| 3388 | defer func() { |
| 3389 | huma.NewError = orig |
| 3390 | }() |
| 3391 | huma.NewError = func(status int, message string, errs ...error) huma.StatusError { |
| 3392 | details := make([]string, len(errs)) |
| 3393 | for i, err := range errs { |
| 3394 | details[i] = err.Error() |
| 3395 | } |
| 3396 | return &MyError{ |
| 3397 | status: status, |
| 3398 | Message: message, |
| 3399 | Details: details, |
| 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | _, api := humatest.New(t, huma.DefaultConfig("Test API", "1.0.0")) |
| 3404 | |
| 3405 | huma.Register(api, huma.Operation{ |
| 3406 | OperationID: "get-error", |
| 3407 | Method: http.MethodGet, |
| 3408 | Path: "/error", |
| 3409 | }, func(ctx context.Context, i *struct{}) (*struct{}, error) { |
| 3410 | return nil, huma.Error404NotFound("not found", errors.New("some-other-error")) |
| 3411 | }) |
| 3412 | |
| 3413 | resp := api.Get("/error", "Host: localhost") |
| 3414 | assert.JSONEq(t, `{"$schema":"http://localhost/schemas/MyError.json","message":"not found","details":["some-other-error"]}`, resp.Body.String()) |
| 3415 | } |
| 3416 | |
| 3417 | type BrokenWriter struct { |
| 3418 | http.ResponseWriter |