MCPcopy
hub / github.com/danielgtaylor/huma / TestCustomError

Function TestCustomError

huma_test.go:3386–3415  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3384}
3385
3386func 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
3417type BrokenWriter struct {
3418 http.ResponseWriter

Callers

nothing calls this directly

Calls 4

NewFunction · 0.92
ErrorMethod · 0.65
GetMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…