forked from mtusi2026/mtusi2026lab5
44 lines
1.5 KiB
Go
44 lines
1.5 KiB
Go
package fiber
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"go.lab260.ru/fielderr"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/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")
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestPublicErrorUnavailable keeps dependency details out of the public envelope.
|
|
func TestPublicErrorUnavailable(t *testing.T) {
|
|
err := publicError(fmt.Errorf("private database detail: %w", service.ErrUnavailable))
|
|
response := errorResponse(err, "request")
|
|
if response.Error.Code != codeUnavailable || response.Error.Message != "dependency unavailable" {
|
|
t.Fatalf("unexpected response: %+v", response)
|
|
}
|
|
}
|