当前位置:网站首页>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/mux
Quick 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: true
2. 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.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 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 .
边栏推荐
- Basic concepts of database
- ECMAScript 6.0
- CX5120控制汇川IS620N伺服报错E15解决方案
- Common interview questions for performance test
- 二叉树神级遍历:Morris遍历
- The shell script uses two bars to receive external parameters
- MySQL index --01--- design principle of index
- 完全背包问题
- Design practice of current limiting components
- MySQL knowledge points
猜你喜欢
随机推荐
第03章_用户与权限管理
JS日常开发小技巧(持续更新)
[linear DP] longest common subsequence
Filter
Nacos
过滤器 Filter
If a parent class defines a parameterless constructor, is it necessary to call super ()?
Introduction to EtherCAT
[us match preparation] complete introduction to word editing formula
EtherCAT原理概述
How to use hybrid format to output ISO files? isohybrid:command not found
二叉树神级遍历:Morris遍历
几行事务代码,让我赔了16万
网页不能右键 F12 查看源代码解决方案
[linear DP] shortest editing distance
10、Scanner.next() 无法读取空格/indexOf -1
别再说不会解决 “跨域“ 问题啦
Leetcode 1482 guess, how about this question?
服务器渲染技术jsp
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动