From cccc7ba58c65098b9dd24db80856285aecfd6d0e Mon Sep 17 00:00:00 2001 From: Dmitry Kotik <7944694+dkotik@users.noreply.github.com> Date: Wed, 17 Apr 2024 16:53:56 +0200 Subject: [PATCH] remove invalid request validation error Validation error type and status code enforcement should be handled at a layer above the adaptor. --- deduplicator_test.go | 2 +- error.go | 17 ----------------- middleware/session/errors.go | 16 +++++++++++++++- unary.go | 2 +- void.go | 2 +- 5 files changed, 18 insertions(+), 21 deletions(-) diff --git a/deduplicator_test.go b/deduplicator_test.go index 5074f34..ad3a1a4 100644 --- a/deduplicator_test.go +++ b/deduplicator_test.go @@ -17,7 +17,7 @@ func TestDeduplicator(t *testing.T) { t.Error(err) } if expect != ok { - t.Errorf("deduplication expection was not met: %v vs %v", expect, ok) + t.Errorf("expectation not met: %v vs %v", expect, ok) } } diff --git a/error.go b/error.go index aa800e5..3163593 100644 --- a/error.go +++ b/error.go @@ -94,23 +94,6 @@ type InvalidRequestError struct { error } -// TODO: add slog.LogValue with more details including "request validation failed" -func NewInvalidRequestError(fromError error) *InvalidRequestError { - return &InvalidRequestError{fromError} -} - -func (e *InvalidRequestError) Error() string { - return e.error.Error() -} - -func (e *InvalidRequestError) Unwrap() error { - return e.error -} - -func (e *InvalidRequestError) HyperTextStatusCode() int { - return http.StatusUnprocessableEntity -} - type DecodingError struct { error } diff --git a/middleware/session/errors.go b/middleware/session/errors.go index aa33411..9a113cb 100644 --- a/middleware/session/errors.go +++ b/middleware/session/errors.go @@ -1,6 +1,9 @@ package session -import "fmt" +import ( + "fmt" + "net/http" +) type Error uint8 @@ -9,6 +12,17 @@ const ( ErrLargeCookie ) +func (e Error) HyperTextStatusCode() int { + switch e { + case ErrNoSessionInContext: + return http.StatusForbidden + case ErrLargeCookie: + return http.StatusUnprocessableEntity + default: + return http.StatusInternalServerError + } +} + func (e Error) Error() string { switch e { case ErrNoSessionInContext: diff --git a/unary.go b/unary.go index 8607b2d..188213e 100644 --- a/unary.go +++ b/unary.go @@ -83,7 +83,7 @@ func (a *UnaryFuncAdaptor[T, V, O]) executeDomainCall( ctx := r.Context() if err = request.Validate(ctx); err != nil { - return NewInvalidRequestError(err) + return err } response, err := a.domainCall(ctx, request) diff --git a/void.go b/void.go index d77e80b..6aaac74 100644 --- a/void.go +++ b/void.go @@ -69,7 +69,7 @@ func (a *VoidFuncAdaptor[T, V]) executeDomainCall( ctx := r.Context() if err = request.Validate(ctx); err != nil { - return NewInvalidRequestError(err) + return err } if err = a.domainCall(ctx, request); err != nil { return err