go
55 lines · 8 steps
Centralized error handling in Gin
A custom error type plus a tail middleware turns scattered handler errors into consistent JSON responses.
Explained by
highlit
1package middleware
2
3import (
4 "errors"
5 "net/http"
6
7 "github.com/gin-gonic/gin"
8)
9
10type APIError struct {
11 Status int `json:"-"`
12 Code string `json:"code"`
13 Message string `json:"message"`
14}
15
16func (e *APIError) Error() string {
17 return e.Message
18}
19
20func NewAPIError(status int, code, message string) *APIError {
21 return &APIError{Status: status, Code: code, Message: message}
22}
23
24var (
25 ErrNotFound = NewAPIError(http.StatusNotFound, "not_found", "resource not found")
26 ErrUnauthorized = NewAPIError(http.StatusUnauthorized, "unauthorized", "authentication required")
27)
28
29func ErrorHandler() gin.HandlerFunc {
30 return func(c *gin.Context) {
31 c.Next()
32
33 last := c.Errors.Last()
34 if last == nil {
35 return
36 }
37
38 var apiErr *APIError
39 if errors.As(last.Err, &apiErr) {
40 c.AbortWithStatusJSON(apiErr.Status, gin.H{"error": apiErr})
41 return
42 }
43
44 if last.Type == gin.ErrorTypeBind {
45 c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
46 "error": gin.H{"code": "invalid_request", "message": last.Err.Error()},
47 })
48 return
49 }
50
51 c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
52 "error": gin.H{"code": "internal_error", "message": "something went wrong"},
53 })
54 }
55}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Implementing the error interface lets a struct carry HTTP status and machine-readable codes while still working as a plain error.
- 2Running logic after c.Next() lets one middleware inspect everything handlers recorded and shape the final response.
- 3errors.As unwraps an error chain so you can branch on a concrete type rather than string-matching messages.
Related explainers
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
python
import time from collections import defaultdict from threading import Lock
Sliding-window login rate limiting in Flask
rate-limiting
sliding-window
thread-safety
Intermediate
7 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
go
package model import ( "encoding/json"
Custom JSON marshaling in Go
json
serialization
interfaces
Intermediate
5 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/centralized-error-handling-in-gin-explained-go-511d/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.