当前位置:网站首页>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 .
边栏推荐
猜你喜欢

LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表

MySQL knowledge points

Metadata in NFT

The best learning method in the world: Feynman learning method

So easy 将程序部署到服务器

pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear

Avalanche problem and the use of sentinel

Analyze datahub, a new generation metadata platform of 4.7K star
![[nine day training] content III of the problem solution of leetcode question brushing Report](/img/7e/1e76181e56ef7feb083f9662df71c7.jpg)
[nine day training] content III of the problem solution of leetcode question brushing Report

Keil5中如何做到 0 Error(s), 0 Warning(s).
随机推荐
Kmeans
服务器渲染技术jsp
Nacos
The 'mental (tiring) process' of building kubernetes/kubesphere environment with kubekey
pytest-fixture
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
Metadata in NFT
网页不能右键 F12 查看源代码解决方案
Chapter 03_ User and authority management
数组的includes( )
ECMAScript 6.0
HTB-Lame
第03章_用戶與權限管理
pytorch训练深度学习网络设置cuda指定的GPU可见
Basic concept and classification of sorting
Elk elegant management server log
过滤器 Filter
Pytest -- plug-in writing
【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
EtherCAT简介