当前位置:网站首页>Gorilla/mux framework (RK boot): RPC error code design
Gorilla/mux framework (RK boot): RPC error code design
2022-07-01 03:27:00 【dongxuny】

Introduce
Through a complete example , How to gorilla/mux Reasonable design under the frame API Error code .
We will use rk-boot To start up gorilla/mux 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/muxQuick start
adopt rk-boot , Users can easily build gorilla/mux Framework microservices ,rk-boot Integrated Panic Capture and standard error types .
1. establish boot.yaml
boot.yaml The document describes gorilla/mux Meta information of framework startup ,rk-boot By reading the boot.yaml To start up gorilla/mux.
---mux: - 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 mainimport ( "context" "github.com/rookie-ninja/rk-boot" "github.com/rookie-ninja/rk-boot/mux" "github.com/rookie-ninja/rk-common/error" "github.com/rookie-ninja/rk-mux/interceptor" "net/http")func main() { // Create a new boot instance. boot := rkboot.NewBoot() // Register handler entry := rkbootmux.GetMuxEntry("greeter") entry.Router.NewRoute().Methods(http.MethodGet).Path("/v1/greeter").HandlerFunc(Greeter) // Bootstrap boot.Bootstrap(context.TODO()) boot.WaitForShutdownSig(context.TODO())}func Greeter(writer http.ResponseWriter, request *http.Request) { err := rkerror.New( rkerror.WithHttpCode(http.StatusAlreadyReported), rkerror.WithMessage("Trigger manually!"), rkerror.WithDetails("This is detail.", false, -1, 0.1)) rkmuxinter.WriteJson(writer, http.StatusAlreadyReported, err)}3. start-up main.go
$ go run main.go4. 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 mainimport ( "context" "github.com/rookie-ninja/rk-boot" "github.com/rookie-ninja/rk-boot/mux" "net/http")func main() { // Create a new boot instance. boot := rkboot.NewBoot() // Register handler entry := rkbootmux.GetMuxEntry("greeter") entry.Router.NewRoute().Methods(http.MethodGet).Path("/v1/greeter").HandlerFunc(Greeter) // Bootstrap boot.Bootstrap(context.TODO()) boot.WaitForShutdownSig(context.TODO())}func Greeter(writer http.ResponseWriter, request *http.Request) { 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 .
边栏推荐
猜你喜欢
随机推荐
Research on target recognition and tracking based on 3D laser point cloud
The best learning method in the world: Feynman learning method
C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
完全背包问题
Detailed explanation of ES6 deconstruction grammar
How to use hybrid format to output ISO files? isohybrid:command not found
不用加减乘除实现加法
ECMAScript 6.0
Keil5中如何做到 0 Error(s), 0 Warning(s).
力扣-两数之和
Cookie&Session
Leetcode 1482 guess, how about this question?
Cookie&Session
排序链表(归并排序)
Mybati SQL statement printing
How to verify whether the contents of two files are the same
Elk elegant management server log
POI导出excel,按照父子节点进行分级显示
A few lines of transaction codes cost me 160000 yuan
Overview of EtherCAT principle








