Skip to content

Commit

Permalink
remove invalid request validation error
Browse files Browse the repository at this point in the history
Validation error type and status code enforcement should be handled at a 
layer above the adaptor.
  • Loading branch information
dkotik committed Apr 17, 2024
1 parent d32a935 commit cccc7ba
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion deduplicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
17 changes: 0 additions & 17 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 15 additions & 1 deletion middleware/session/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package session

import "fmt"
import (
"fmt"
"net/http"
)

type Error uint8

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion unary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion void.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cccc7ba

Please sign in to comment.