gf icon indicating copy to clipboard operation
gf copied to clipboard

Question: I wanna change http response Code in `gcode` , What is the best way for me to do it ?

Open zhonghuaxunGM opened this issue 2 years ago • 2 comments

1. What version of Go and system type/arch are you using?

go version go1.16.2 windows/amd64

2. What version of GoFrame are you using?

github.com/gogf/gf/v2 v2.0.0-rc3

3. Can this issue be re-produced with the latest release?

yes

4. What did you do?

I wanna change http response Code in my way . I found the http Code in goframe errors\gcode\gcode.go

var (
	CodeNil                      = localCode{-1, "", nil}                            // No error code specified.
	CodeOK                       = localCode{0, "OK", nil}                           // It is OK.
	CodeInternalError            = localCode{50, "Internal Error", nil}              // An error occurred internally.
	CodeValidationFailed         = localCode{51, "Validation Failed", nil}           // Data validation failed.
         ........
	CodeBusinessValidationFailed = localCode{300, "Business Validation Failed", nil} // Business validation failed.
)

I wanna change CodeValidationFailed = localCode{51, "Validation Failed", nil} into CodeValidationFailed = localCode{400, "Validation Failed and xxxxxxx", nil} .

What is the best way for me to do it ?

P.S. I also implment the MiddlewareHandlerResponse in group.Middleware() and Code Struct in my own way just like gcode. But Finally I found CodeValidationFailed used in gvalid which I really want the gvalid functions and abilities .


zhonghuaxunGM avatar May 18 '22 09:05 zhonghuaxunGM

I also want to know

weni09 avatar Jul 06 '22 08:07 weni09

@zhonghuaxunGM You can change the error code in middleware. The internal error code can be retrieved using gerror.Code(r.GetError()), you can use switch ... case ... mechanism to do the change.

gqcn avatar Aug 16 '22 13:08 gqcn