retcode.Error() from go generate
This commit is contained in:
parent
201bf2374b
commit
4d0eb1d95a
|
@ -1,12 +1,31 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type RetCode int
|
||||
|
||||
// Reserved return codes
|
||||
const (
|
||||
RetCodeOK = RetCode(0)
|
||||
RetCodeInternalError = RetCode(1)
|
||||
RetCodeUnauthorized = RetCode(2)
|
||||
RetCodeInsufficientFees = RetCode(3)
|
||||
RetCodeUnknownRequest = RetCode(4)
|
||||
RetCodeOK RetCode = 0
|
||||
RetCodeInternalError RetCode = 1
|
||||
RetCodeUnauthorized RetCode = 2
|
||||
RetCodeInsufficientFees RetCode = 3
|
||||
RetCodeUnknownRequest RetCode = 4
|
||||
)
|
||||
|
||||
func (r RetCode) Error() error {
|
||||
switch r {
|
||||
case RetCodeOK:
|
||||
return nil
|
||||
default:
|
||||
return errors.New(r.String())
|
||||
}
|
||||
}
|
||||
|
||||
//go:generate stringer -type=RetCode
|
||||
|
||||
// NOTE: The previous comment generates r.String().
|
||||
// To run it, `go get golang.org/x/tools/cmd/stringer`
|
||||
// and `go generate` in tmsp/types
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
// generated by stringer -type=RetCode; DO NOT EDIT
|
||||
|
||||
package types
|
||||
|
||||
import "fmt"
|
||||
|
||||
const _RetCode_name = "RetCodeOKRetCodeInternalErrorRetCodeUnauthorizedRetCodeInsufficientFeesRetCodeUnknownRequest"
|
||||
|
||||
var _RetCode_index = [...]uint8{0, 9, 29, 48, 71, 92}
|
||||
|
||||
func (i RetCode) String() string {
|
||||
if i < 0 || i+1 >= RetCode(len(_RetCode_index)) {
|
||||
return fmt.Sprintf("RetCode(%d)", i)
|
||||
}
|
||||
return _RetCode_name[_RetCode_index[i]:_RetCode_index[i+1]]
|
||||
}
|
Loading…
Reference in New Issue