前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gorilla/mux 框架(rk-boot): RPC 错误码设计

gorilla/mux 框架(rk-boot): RPC 错误码设计

原创
作者头像
尹东勋
发布2022-02-15 17:16:36
5370
发布2022-02-15 17:16:36
举报

介绍

本文通过一个完整的例子,介绍如何在 gorilla/mux 框架下设计合理的 API 错误码。

我们将会使用 rk-boot 来启动 gorilla/mux 微服务。

请访问如下地址获取完整教程:

考虑范围

一个合理的 RPC 错误,需要考虑如下几个方面。

  • 包含错误码,错误信息
  • 错误信息可扩展
  • 考虑可读性
  • 可解析性,即,用户可通过代码解析错误码,并采取有效行为
  • 避免内部错误益处,例如,Nil point error

错误码结构

{
    "error":{
        "code":500,
        "status":"Internal Server Error",
        "message":"Panic manually!",
        "details":[]
    }
}

安装

go get github.com/rookie-ninja/rk-boot/mux

快速开始

通过 rk-boot ,用户可以轻松搭建 gorilla/mux 框架微服务,rk-boot 集成了 Panic 捕捉以及标准错误类型。

完整例子

1.创建 boot.yaml

boot.yaml 文件描述了 gorilla/mux 框架启动的元信息,rk-boot 通过读取 boot.yaml 来启动 gorilla/mux

---
mux:
  - name: greeter
    port: 8080
    enabled: true

2.创建 main.go

让 /v1/greeter 返回一个错误。

// 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/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.启动 main.go

$ go run main.go

4.验证

$ 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
        ]
    }
}

捕获 Panic(系统崩溃)

我们还是以 demo 代码为例子。

在 RPC 实现中,我们试着让系统崩溃,看看 rk-boot 会如何自动捕获,并且返回何种信息给用户。

1.修改 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/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.验证

$ curl "localhost:8080/v1/greeter?name=rk-dev"
{
    "error":{
        "code":500,
        "status":"Internal Server Error",
        "message":"Panic manually!",
        "details":[]
    }
}

源代码

rk-boot 里对于错误的处理,实现于 rk-common/error 中。

更多例子

请参考:rk-demo 获取更多例子。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 考虑范围
  • 错误码结构
  • 安装
  • 快速开始
    • 1.创建 boot.yaml
      • 2.创建 main.go
        • 3.启动 main.go
          • 4.验证
          • 捕获 Panic(系统崩溃)
            • 1.修改 main.go
              • 2.验证
              • 源代码
              • 更多例子
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档