Categorygithub.com/ikaiguang/protoc-gen-go-errors
modulepackage
0.0.3
Repository: https://github.com/ikaiguang/protoc-gen-go-errors.git
Documentation: pkg.go.dev

# README

protoc-gen-go-errors

自定义 go-kratos/kratos 错误;

当前版本: version

protoc-gen-go-errors protoc-gen-go-errors

安装覆盖 kratos errors 工具

kratos errorsthis errors
v2.7.3go install github.com/ikaiguang/[email protected]
v2.7.3go install github.com/ikaiguang/[email protected]
v2.8.0go install github.com/ikaiguang/[email protected]

Protobuf 定义如下:

// ERROR .
enum ERROR {
  option (errors.default_code) = 500;

  // 未知错误
  UNKNOWN = 0 [(errors.code) = 500];
}

生成的代码如下:


// Code generated by protoc-gen-go-errors. DO NOT EDIT.

package my_package

import (
	fmt "fmt"
	errors "github.com/go-kratos/kratos/v2/errors"
	strconv "strconv"
)

// This is a compile-time assertion to ensure that this generated file
// is compatible with the kratos package it is being compiled against.
const _ = errors.SupportPackageIsVersion1

var ERROR_http_code = map[string]int{
	"UNKNOWN": 500,
}

// ERROR .
func (x ERROR) HTTPCode() int {
	if v, ok := ERROR_http_code[x.String()]; ok {
		return v
	}
	return 0
}

// 未知错误
func IsUnknown(err error) bool {
	if err == nil {
		return false
	}
	e := errors.FromError(err)
	return e.Reason == ERROR_UNKNOWN.String() && e.Code == 500
}

// 未知错误
func ErrorUnknown(format string, args ...interface{}) *errors.Error {
	e := errors.New(500, ERROR_UNKNOWN.String(), fmt.Sprintf(format, args...))
	e.Metadata = map[string]string{"reason": strconv.Itoa(int(ERROR_UNKNOWN.Number()))}
	return e
}

// 未知错误
func DefaultUnknown_Error() *errors.Error {
	e := errors.New(500, ERROR_UNKNOWN.String(), "未知错误")
	e.Metadata = map[string]string{"reason": strconv.Itoa(int(ERROR_UNKNOWN.Number()))}
	return e
}

# Packages

No description provided by the author