Error Handling
Error handling is an indispensable part of services. Normally, if a http status code is not in the 2xx
series, it can be considered as an http request error.
The error message of a response is returned in plain text. In addition, some other types of errors were defined and retured. Common practice is to pass two fields: code
and msg
that are used to describe the business processing results, and the response body can be formatted as json.
#
Business error response formatNormal business processing
An error occurred in the business processing
#
Login of the user APIsPreviously, when we handled the login logic if the username did not exist, an error was directly returned. Let's try the login
and pass a username that does not exist to see the result.
Next we will return it in json format
#
Custom errorFirst add a
baseerror.go
file in common and fill in the codeReplace errors in login logic with CodeError custom errors
Use custom errors
Restart service verification