Sending HTTP Error Codes With Golang and Gin Gonic

HTTPSending HTTP Error Codes With Golang and Gin GonicA simple way to handle errors in APIsGo is a very popular language for good reason. It is a simple language with performance as good as a low-level language like C++. Acceptance of Golang in web dev…

HTTPSending HTTP Error Codes With Golang and Gin Gonic

A simple way to handle errors in APIs

Go is a very popular language for good reason. It is a simple language with performance as good as a low-level language like C++. Acceptance of Golang in web development is booming at a high rate.

The Gin web framework uses a custom version of HttpRouter, that navigates through your API routes faster than most frameworks out there. Gin claims to be 40 times faster than Martini.

While developing APIs for your service you might want a way to handle the errors/exceptions and return appropriate HTTP error code and message to the client. The following scheme makes it very easy to handle such situations.

Response Dispatcher

Implementation of response dispatcher looks like this:

The above method accepts the Context and Response structure that generalizes the response.

This method can be used to send various HTTP error codes along with error messages to the clients. Usage of the dispatcher looks like this:

Wrapping it up

There are thousands of ways to send Error responses from the APIs, this is one of those. Whenever I start a new project, I prefer a method like this to make my life easy.


Sending HTTP Error Codes With Golang and Gin Gonic was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Amrit Pal Singh | Sciencx (2024-03-29T12:08:45+00:00) » Sending HTTP Error Codes With Golang and Gin Gonic. Retrieved from https://www.scien.cx/2021/06/24/sending-http-error-codes-with-golang-and-gin-gonic/.
MLA
" » Sending HTTP Error Codes With Golang and Gin Gonic." Amrit Pal Singh | Sciencx - Thursday June 24, 2021, https://www.scien.cx/2021/06/24/sending-http-error-codes-with-golang-and-gin-gonic/
HARVARD
Amrit Pal Singh | Sciencx Thursday June 24, 2021 » Sending HTTP Error Codes With Golang and Gin Gonic., viewed 2024-03-29T12:08:45+00:00,<https://www.scien.cx/2021/06/24/sending-http-error-codes-with-golang-and-gin-gonic/>
VANCOUVER
Amrit Pal Singh | Sciencx - » Sending HTTP Error Codes With Golang and Gin Gonic. [Internet]. [Accessed 2024-03-29T12:08:45+00:00]. Available from: https://www.scien.cx/2021/06/24/sending-http-error-codes-with-golang-and-gin-gonic/
CHICAGO
" » Sending HTTP Error Codes With Golang and Gin Gonic." Amrit Pal Singh | Sciencx - Accessed 2024-03-29T12:08:45+00:00. https://www.scien.cx/2021/06/24/sending-http-error-codes-with-golang-and-gin-gonic/
IEEE
" » Sending HTTP Error Codes With Golang and Gin Gonic." Amrit Pal Singh | Sciencx [Online]. Available: https://www.scien.cx/2021/06/24/sending-http-error-codes-with-golang-and-gin-gonic/. [Accessed: 2024-03-29T12:08:45+00:00]
rf:citation
» Sending HTTP Error Codes With Golang and Gin Gonic | Amrit Pal Singh | Sciencx | https://www.scien.cx/2021/06/24/sending-http-error-codes-with-golang-and-gin-gonic/ | 2024-03-29T12:08:45+00:00
https://github.com/addpipe/simple-recorderjs-demo