Skip to content

About

Handling with errors in gin-gonic

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Handling errors in gin-gonic

Starting application

go mod dowload
go run main.go

Request

curl --location --request POST 'http://localhost:8080/api' \
--header 'Content-Type: application/json' \
--data-raw '{
    "id": "some-id",
    "fail_database": false,
    "fail_service": false
}'

Errors

There are two errors struct: database.Err and service.Err. They implement error interface Error() string and Is(target error) bool, returning true if target is pkg.InternalErr.

Middleware

The middleware ErrorHandlerMiddleware() get the errors from gin-gonic context. If present, check if it is a pkg.InternalErr. Case true, return an internal server error; otherwise, return bad request.
This middleware also checks if err.Err is a database.Err and prints additional logs.

Handler

The api.Handler call run c.Error(err) to append errors in gin-gonic context.

Additional information

About

Handling with errors in gin-gonic

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages