template-monorepo/internal/response/request_test.go

61 lines
1.5 KiB
Go
Raw Permalink Normal View History

2026-05-19 11:00:28 +00:00
package response_test
import (
"errors"
"net/http"
"testing"
errs "gateway/internal/library/errors"
"gateway/internal/library/errors/code"
2026-05-19 12:56:32 +00:00
"gateway/internal/library/validate"
2026-05-19 11:00:28 +00:00
"gateway/internal/response"
)
func TestWrapRequestError(t *testing.T) {
t.Parallel()
response.RequestErrScope = code.Facade
e := response.WrapRequestError(errors.New("field id is required"))
be := errs.FromError(e)
if be == nil {
t.Fatal("expected business error")
}
if be.Category() != code.InputInvalidFormat {
t.Fatalf("category = %d, want %d", be.Category(), code.InputInvalidFormat)
}
if be.HTTPStatus() != http.StatusBadRequest {
t.Fatalf("http = %d, want 400", be.HTTPStatus())
}
}
2026-05-19 12:56:32 +00:00
func TestWrapRequestErrorValidationErrors(t *testing.T) {
t.Parallel()
response.RequestErrScope = code.Facade
ve := validate.ValidationErrors{
{Field: "email", Message: "email must be a valid email address"},
}
e := response.WrapRequestError(ve)
be := errs.FromError(e)
if be == nil {
t.Fatal("expected business error")
}
if be.Category() != code.InputInvalidFormat {
t.Fatalf("category = %d, want %d", be.Category(), code.InputInvalidFormat)
}
if be.HTTPStatus() != http.StatusBadRequest {
t.Fatalf("http = %d, want 400", be.HTTPStatus())
}
}
2026-05-19 11:00:28 +00:00
func TestWrapRequestErrorPreservesBusinessError(t *testing.T) {
t.Parallel()
orig := errs.For(code.Facade).ResNotFound("x")
2026-05-19 13:15:18 +00:00
if wrapped := response.WrapRequestError(orig); !errors.Is(wrapped, orig) {
2026-05-19 11:00:28 +00:00
t.Fatal("business error should not be wrapped")
}
}