Files
mtusi2026lab2/internal/controller/http/fiber/public_errors_test.go
T
Vladislav Agapov b90581f613
Application / verify (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
Implement lab 2 with PostgreSQL, Valkey admission and Helm deployment
2026-10-15 17:09:26 +03:00

35 lines
1.1 KiB
Go

package fiber
import (
"errors"
"fmt"
"testing"
"go.lab260.ru/fielderr"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab2/internal/service"
)
// TestPublicErrorSafety maps wrapped application failures without exposing internal details.
func TestPublicErrorSafety(t *testing.T) {
t.Run("wrapped application error", func(t *testing.T) {
err := publicError(fmt.Errorf("private database detail: %w", service.ErrConflict))
response := errorResponse(err, "request")
if response.Error.Code != codeConflict || response.Error.Message != "username already exists" {
t.Fatalf("unexpected response: %+v", response)
}
})
t.Run("unknown error", func(t *testing.T) {
err := publicError(errors.New("private database detail"))
if err != errInternal {
t.Fatalf("unexpected public error: %v", err)
}
})
t.Run("foreign public status", func(t *testing.T) {
err := fielderr.WithStatus(errors.New("private detail"), fielderr.NewStatusCode(codeInvalid), "private detail")
if publicError(err) != errInternal {
t.Fatal("untrusted error metadata reached the public response")
}
})
}