Code Monkey home page Code Monkey logo

proto-err-reason's Introduction

proto-err-reason

trans proto error reason into err code

将 proto 中的 error reason 转换为 go error code 文件

比如你的 error reason proto 文件定义如下:

syntax = "proto3";

package role.v1;

option go_package = "rolesvc/api/v1;v1";

enum ErrorReason {
  ERROR_REASON_UNSPECIFIED = 0; // unknown
  USER_NOT_FOUND = 1;// not found
  Role_Create_Fail = 300001001;// 新增角色失败
}

通过执行 eTrans -file=example/t1.proto -o=example/output_error_code.go -pkg=example

可以将其转换为:

package example

type ErrCode int

const (
	ERROR_REASON_UNSPECIFIED ErrCode = 0 // unknown
	USER_NOT_FOUND ErrCode = 1 // not found
	Role_Create_Fail ErrCode = 300001001 // 新增角色失败

)

至此,再执行 cd example && go generate 会生成 code_string.go 文件:

// Code generated by "stringer -type ErrCode -linecomment -output code_string.go"; DO NOT EDIT.

package example

import "strconv"

func _() {
	// An "invalid array index" compiler error signifies that the constant values have changed.
	// Re-run the stringer command to generate them again.
	var x [1]struct{}
	_ = x[ERROR_REASON_UNSPECIFIED-0]
	_ = x[USER_NOT_FOUND-1]
	_ = x[Role_Create_Fail-300001001]
}

const (
	_ErrCode_name_0 = "unknownnot found"
	_ErrCode_name_1 = "新增角色失败"
)

var (
	_ErrCode_index_0 = [...]uint8{0, 7, 16}
)

func (i ErrCode) String() string {
	switch {
	case 0 <= i && i <= 1:
		return _ErrCode_name_0[_ErrCode_index_0[i]:_ErrCode_index_0[i+1]]
	case i == 300001001:
		return _ErrCode_name_1
	default:
		return "ErrCode(" + strconv.FormatInt(int64(i), 10) + ")"
	}
}

利用 stringer 工具,可以为 output_error_code.go 中的错误定义生成对应的描述实现(String() 方法)。

当你使用它时,可以直接用 fmt.Printf("%s", example.Role_Create_Fail) 就能打印出错误描述。

综上,你只需在 proto 文件中(如 example/t1.proto)进行错误定义,就能直接生成一套 go 代码中可以使用的错误码错误描述

reference

proto-err-reason's People

Contributors

suhanyujie avatar

Stargazers

27 avatar opensite avatar

Watchers

James Cloos avatar

Forkers

wnz27

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.