当前位置:网站首页>Gin framework: RPC error code design
Gin framework: RPC error code design
2022-06-24 02:31:00 【Trespass 】
Introduce
Through a complete example , How to Gin Reasonable design under the frame API Error code .
We will use rk-boot To start up Gin Framework based microservices .
Please visit the following address for a complete tutorial :
The scope of consideration
A reasonable RPC error , The following aspects need to be considered .
- Contains error code , error message
- Error messages are extensible
- Consider readability
- Analyzability , namely , The user can parse the error code through the code , And take effective actions
- The benefits of avoiding internal errors , for example ,Nil point error
Error code structure
{
"error":{
"code":500,
"status":"Internal Server Error",
"message":"Panic manually!",
"details":[]
}
}install
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-gin
Quick start
adopt rk-boot , Users can easily build Gin Framework microservices ,rk-boot Integrated Panic Capture and standard error types .
1. establish boot.yaml
boot.yaml The document describes Gin Original information of framework startup ,rk-boot By reading the boot.yaml To start up Gin.
---
gin:
- name: greeter
port: 8080
enabled: true2. establish main.go
Give Way /v1/greeter Return an error .
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"github.com/gin-gonic/gin"
"github.com/rookie-ninja/rk-boot"
"github.com/rookie-ninja/rk-gin/boot"
"net/http"
)
// @title RK Swagger for Gin
// @version 1.0
// @description This is a greeter service with rk-boot.
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Register handler
boot.GetEntry("greeter").(*rkgin.GinEntry).Router.GET("/v1/greeter", Greeter)
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
// @Summary Greeter service
// @Id 1
// @version 1.0
// @produce application/json
// @Param name query string true "Input name"
// @Success 200 {object} GreeterResponse
// @Router /v1/greeter [get]
func Greeter(ctx *gin.Context) {
err := rkerror.New(
rkerror.WithHttpCode(http.StatusAlreadyReported),
rkerror.WithMessage("Trigger manually!"),
rkerror.WithDetails("This is detail.", false, -1, 0.1))
ctx.JSON(http.StatusAlreadyReported, err)
}
// Response.
type GreeterResponse struct {
Message string
}3. start-up main.go
$ go run main.go
4. verification
$ curl "localhost:8080/v1/greeter?name=rk-dev"
{
"error":{
"code":208,
"status":"Already Reported",
"message":"Trigger manually!",
"details":[
"This is detail.",
false,
-1,
0.1
]
}
}Capture Panic( System crash )
We still use demo Code as an example .
stay RPC In the implementation , We tried to crash the system , have a look rk-boot How will it be captured automatically , And what information is returned to the user .
1. modify main.go
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"github.com/gin-gonic/gin"
"github.com/rookie-ninja/rk-boot"
"github.com/rookie-ninja/rk-gin/boot"
)
// @title RK Swagger for Gin
// @version 1.0
// @description This is a greeter service with rk-boot.
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Register handler
boot.GetEntry("greeter").(*rkgin.GinEntry).Router.GET("/v1/greeter", Greeter)
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
// @Summary Greeter service
// @Id 1
// @version 1.0
// @produce application/json
// @Param name query string true "Input name"
// @Success 200 {object} GreeterResponse
// @Router /v1/greeter [get]
func Greeter(ctx *gin.Context) {
panic("Panic manually!")
}2. verification
$ curl "localhost:8080/v1/greeter?name=rk-dev"
{
"error":{
"code":500,
"status":"Internal Server Error",
"message":"Panic manually!",
"details":[
]
}
}Source code
rk-boot Error handling in , To achieve in rk-common/error in .
More examples
Please refer to :rk-demo Get more examples .
边栏推荐
- Can cloud computing scale flexibly? What are the characteristics of elasticity?
- UNIX command encyclopedia, common commands are here, work must!
- Buddha's foot before examination: the second play of leetcode
- What are the general contents of the enterprise website construction scheme
- How to obtain the information of easydss single / multiple live streams through the API interface?
- How to formulate a domain name trademark registration scheme? What if the plan is rejected?
- What is the domain name trademark? What are the registration conditions for domain names and trademarks?
- Which is a good human voice synthesis platform? What are the human voice synthesis application scenarios
- Case of data recovery by misoperation under NTFS file system
- Introduction to development model + test model
猜你喜欢
Cloudpods golang practice

If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations

163 mailbox login portal display, enterprise mailbox computer version login portal

How to fill in and register e-mail, and open mass mailing software for free

Leetcode969: pancake sorting (medium, dynamic programming)

Advanced BOM tool intelligent packaging function

2020 language and intelligent technology competition was launched, and Baidu provided the largest Chinese data set

application. Yaml configuring multiple running environments

Introduction to development model + test model
随机推荐
What are the main functions of DNS? What are the benefits of IP address translation
What about registered domain names? How long does it take to register a domain name?
S2b2c platform of fresh food industry reshapes the ecosystem of agricultural products and builds a mobile fresh food comprehensive mall
How to improve the success rate of trademark registration? How long does it take to register a trademark?
Data backup is required for manual upgrade of WordPress website program
A complete collection of SQL commands. Each command has an example. Xiaobai can become a God after reading it!
application. Yaml configuring multiple running environments
NFT metauniverse and the relationship between Games Golden Finance
Layout use case
Frequent screen flashing after VNC login - abnormal system time
Learning and life -- Talking about my learning methods
Vivo global mall: design and practice of commodity system architecture
Objective-C downloads pictures from the network, saves them, and displays them from the save with uiimageview
2021 game security industry summit: Security Co Construction and healthy development of escort industry
Flink practice tutorial: getting started 1- zero basic users realize simple Flink tasks
How to apply for top-level domain names? What are the types of top-level domain names?
What is ITF barcode
How to build your own website? Is it difficult?
SQL Server database recovery case analysis
Leetcode969: pancake sorting (medium, dynamic programming)