Implement consistent error handling patterns throughout your codebase to improve readability and maintainability. **Key guidelines:** 1. Use named error variables instead of inline error creation
Implement consistent error handling patterns throughout your codebase to improve readability and maintainability.
Key guidelines:
// Instead of
return errors.New("invalid request")
// Use
var ErrInvalidRequest = errors.New("invalid request")
return ErrInvalidRequest
// Instead of
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
} else {
c.JSON(http.StatusOK, gin.H{
"result": data,
})
}
// Use
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"result": data,
})
// Instead of duplicating similar code
if errors.As(err, &maxBytesErr) {
c.AbortWithError(http.StatusRequestEntityTooLarge, err).SetType(ErrorTypeBind)
} else {
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind)
}
// Use
var statusCode int
switch {
case errors.As(err, &maxBytesErr):
statusCode = http.StatusRequestEntityTooLarge
default:
statusCode = http.StatusBadRequest
}
c.AbortWithError(statusCode, err).SetType(ErrorTypeBind)
Consistent error handling patterns make code more predictable, reduce bugs, and make it easier for other developers to understand error flows.
Enter the URL of a public GitHub repository