Skip to content

Commit 6e3f4fe

Browse files
JoesHageSebastienMelki
authored andcommitted
Add test for structured response and fix linting issues
1 parent 9dc1433 commit 6e3f4fe

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

examples/error-handler/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,16 @@ func CombinedErrorHandler(w http.ResponseWriter, r *http.Request, err error) pro
241241
w.Header().Set("X-Request-ID", requestID)
242242
w.Header().Set("X-Error-Timestamp", time.Now().UTC().Format(time.RFC3339))
243243

244-
// Handle not found errors - return NotFoundError proto message from errors.proto
244+
// Handle proto NotFoundError returned directly from handler
245+
// The framework preserves its structure automatically
246+
var protoNotFound *models.NotFoundError
247+
if errors.As(err, &protoNotFound) {
248+
w.Header().Set("X-Error-Type", "not_found")
249+
w.WriteHeader(http.StatusNotFound)
250+
return nil // Return nil to let framework use the proto error directly
251+
}
252+
253+
// Handle Go struct not found errors (legacy pattern)
245254
var notFound *ErrNotFound
246255
if errors.As(err, &notFound) {
247256
w.Header().Set("X-Error-Type", "not_found")

internal/httpgen/error_handler_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,10 @@ func TestProtoMessageErrorPreservation(t *testing.T) {
461461
return
462462
}
463463

464-
if !(valErrPos < handlerErrPos && handlerErrPos < protoMsgPos && protoMsgPos < fallbackPos) {
465-
t.Error("defaultErrorResponse should check errors in order: ValidationError, Error, proto.Message, fallback")
464+
if valErrPos >= handlerErrPos || handlerErrPos >= protoMsgPos || protoMsgPos >= fallbackPos {
465+
t.Error(
466+
"defaultErrorResponse should check errors in order: ValidationError, Error, proto.Message, fallback",
467+
)
466468
}
467469
})
468470
}

0 commit comments

Comments
 (0)