You've already forked agentic-coding-workflow
feat(errs): AppError type with HTTP status mapping
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package errs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNew_BasicShape(t *testing.T) {
|
||||
e := New(CodeInvalidInput, "字段缺失")
|
||||
require.Equal(t, CodeInvalidInput, e.Code)
|
||||
require.Equal(t, "字段缺失", e.Message)
|
||||
require.Nil(t, e.Cause)
|
||||
}
|
||||
|
||||
func TestWithFields(t *testing.T) {
|
||||
e := New(CodeInvalidInput, "x").WithFields(map[string]any{"name": "required"})
|
||||
require.Equal(t, "required", e.Fields["name"])
|
||||
}
|
||||
|
||||
func TestWrap_PreservesCause(t *testing.T) {
|
||||
cause := errors.New("boom")
|
||||
e := Wrap(cause, CodeInternal, "wrapped")
|
||||
require.ErrorIs(t, e, cause)
|
||||
require.Equal(t, "wrapped: boom", e.Error())
|
||||
}
|
||||
|
||||
func TestHTTPStatus(t *testing.T) {
|
||||
cases := map[Code]int{
|
||||
CodeInvalidInput: http.StatusBadRequest,
|
||||
CodeUnauthorized: http.StatusUnauthorized,
|
||||
CodeForbidden: http.StatusForbidden,
|
||||
CodeNotFound: http.StatusNotFound,
|
||||
CodeConflict: http.StatusConflict,
|
||||
CodeRateLimited: http.StatusTooManyRequests,
|
||||
CodeUpstream: http.StatusBadGateway,
|
||||
CodeUnavailable: http.StatusServiceUnavailable,
|
||||
CodeInternal: http.StatusInternalServerError,
|
||||
}
|
||||
for code, want := range cases {
|
||||
require.Equal(t, want, HTTPStatus(code), "code=%s", code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user