From e73ee2e387aeb004c4e441d56ca1caf4c40e231d Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Wed, 19 Mar 2025 22:46:35 +0800 Subject: [PATCH] fix: improve error handling in the error hook - Updated the error handling logic in the tonic error hook to unwrap nested errors, ensuring that the most relevant error message is returned. - Enhanced the clarity of the error response by directly using the unwrapped error message, improving debugging and user feedback. --- core/controllers/base.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/controllers/base.go b/core/controllers/base.go index 2ec01277..47a91206 100644 --- a/core/controllers/base.go +++ b/core/controllers/base.go @@ -18,8 +18,12 @@ import ( func init() { tonic.SetErrorHook(func(context *gin.Context, err error) (int, interface{}) { + unwrappedErr := errors.Unwrap(err) + if unwrappedErr != nil { + err = unwrappedErr + } response := gin.H{ - "error": errors.Unwrap(err).Error(), + "error": err.Error(), } status := http.StatusInternalServerError constErr, ok := errors.AsType[errors.ConstError](err)