init: v1.0.0

This commit is contained in:
yaole
2026-05-27 23:03:00 +08:00
commit 8d97f750eb
466 changed files with 80067 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
package gerrors
// 模块错误码定义
const (
XDX_JELLY_GCL = 0x01000000
XDX_JELLY_GCL_SM_SM1 = 0x01001000
XDX_JELLY_GCL_SM_SM2 = 0x01002000
XDX_JELLY_GCL_SM_SM3 = 0x01003000
XDX_JELLY_GCL_SM_SM4 = 0x01004000
XDX_JELLY_GCL_SM_SM9 = 0x01009000
XDX_JELLY_GCL_SM = 0x0100a000
XDX_JELLY_GCL_UTILS_BLOCKMODE = 0x0100b000
XDX_JELLY_GCL_SHARING_SSSS = 0x0100c000
XDX_JELLY_GCL_GMATH = 0x0100d000
XDX_JELLY_GCL_PBKB = 0x0100e000
XDX_JELLY_GCL_TPC_SM2_SM2m = 0x0100f000
XDX_JELLY_GCL_TPC_SM2_SM2a = 0x01010000
XDX_JELLY_GCL_TPC_SM9_SM9m = 0x01011000
XDX_JELLY_GCL_GRAND = 0x01012000
XDX_JELLY_GCL_GMPKI_SXF = 0x02000000
XDX_JELLY_GCL_GMPKI_X509V3 = 0x02001000
XDX_JELLY_GCL_GMPKI_IBC = 0x02002000
XDX_JELLY_GCL_GMPKI_GSSL = 0x02003000
XDX_JELLY_GCL_GMPKI_CERTLESS_GMCERTLESS = 0x02004000
)
+46
View File
@@ -0,0 +1,46 @@
package gerrors
import (
"errors"
"fmt"
"testing"
)
var err0 = errors.New("err0")
var err1 = errors.New("err1")
func fn0() error {
return WithStack(WithAnnotating(err0, "comment0"))
}
func fn1() error {
if err := fn0(); err != nil {
e := ChainErrors(err1, err)
return WithStack(WithAnnotating(e, "comment1"))
}
return nil
}
func fn2() error {
if err := fn1(); err != nil {
return WithMessage(err, "err2")
}
return nil
}
func TestError(t *testing.T) {
e := WithAnnotating(err0, "comment0")
fmt.Printf("%+v\n", e)
fmt.Println()
if !Is(fn2(), err0) || !Is(fn2(), err1) {
t.Fail()
}
fmt.Println(fn2())
fmt.Println()
// output:
// err2, caused by err1(comment1), caused by err0(comment0)
fmt.Printf("%+v\n", fn2())
}
+144
View File
@@ -0,0 +1,144 @@
package gerrors
import (
"fmt"
"io"
"xdx.jelly/xgcl/gerrors/internal/errors" //github.com/pkg/errors
)
// exports github.com/pkg/errors's API
var As = errors.As
var Is = errors.Is
var Cause = errors.Cause
var Unwrap = errors.Unwrap
var WithMessage = errors.WithMessage
var WithMessagef = errors.WithMessagef
var WithStack = errors.WithStack
var Wrap = errors.Wrap
var Wrapf = errors.Wrapf
// Unused
// var Errorf = errors.Errorf
// var New = errors.New
// type Frame = errors.Frame
// type StackTrace = errors.StackTrace
func Format(code uint32, msg string) string {
return fmt.Sprintf("[0x%08x]%s", code, msg)
}
type chainedError struct {
err error
cause error
}
func (e *chainedError) Error() string {
return fmt.Sprintf("%s, caused by %s", e.err, e.cause)
}
func (e *chainedError) Cause() error {
return e.cause
}
func (e *chainedError) Unwrap() error {
return e.cause
}
// 定义fmt.Printf("%+v", e)打印格式
func (e *chainedError) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
if e := e.Cause(); e != nil {
fmt.Fprintf(s, "%+v\n", e)
}
_, _ = io.WriteString(s, e.err.Error())
return
}
fallthrough
case 's':
_, _ = io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
}
}
func (e *chainedError) Is(target error) bool {
return Is(e.err, target) || Is(e.cause, target)
}
func ChainErrors(e error, cause error) error {
return &chainedError{
err: e,
cause: cause,
}
}
// withAnnotating wraps an error with annotatings.
type withAnnotating struct {
err error
annotating string
}
func (e *withAnnotating) Error() string {
return fmt.Sprintf("%s(%s)", e.err, e.annotating)
}
func (e *withAnnotating) Cause() error {
return Cause(e.err)
}
func (e *withAnnotating) Unwrap() error {
return Unwrap(e.err)
}
func (e *withAnnotating) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
if e := e.Cause(); e != nil {
fmt.Fprintf(s, "%+v\n", e)
}
io.WriteString(s, e.Error())
return
}
fallthrough
case 's':
_, _ = io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
}
}
func (e *withAnnotating) Is(target error) bool {
return Is(e.err, target)
}
// WithAnnotating add an annotating to err
func WithAnnotating(err error, annotating string) error {
switch err := err.(type) {
case *chainedError:
err.err = WithAnnotating(err.err, annotating)
return err
default:
return &withAnnotating{
err: err,
annotating: annotating,
}
}
}
// WithAnnotatingf add an format annotating to err
func WithAnnotatingf(err error, format string, args ...interface{}) error {
return WithAnnotating(err, fmt.Sprintf(format, args...))
}
type ErrorCode uint32
func (e ErrorCode) Error() string {
return Format(uint32(e), "")
}
func (e ErrorCode) Code() uint32 {
return uint32(e)
}
+24
View File
@@ -0,0 +1,24 @@
# 错误码基线
"xdx.jelly/xgcl": 0x01000000,
"xdx.jelly/xgcl/sm/sm1": 0x01001000,
"xdx.jelly/xgcl/sm/sm2": 0x01002000,
"xdx.jelly/xgcl/sm/sm3": 0x01003000,
"xdx.jelly/xgcl/sm/sm4": 0x01004000,
"xdx.jelly/xgcl/sm/sm9": 0x01009000,
"xdx.jelly/xgcl/sm": 0x0100a000,
"xdx.jelly/xgcl/utils/blockmode": 0x0100b000,
"xdx.jelly/xgcl/sharing/ssss": 0x0100c000,
"xdx.jelly/xgcl/gmath": 0x0100d000,
"xdx.jelly/xgcl/pbkd": 0x0100e000,
"xdx.jelly/xgcl/tpc/sm2/sm2m": 0x0100f000,
"xdx.jelly/xgcl/tpc/sm2/sm2a": 0x01010000,
"xdx.jelly/xgcl/tpc/sm9/sm9m": 0x01011000,
"xdx.jelly/xgcl/grand": 0x01012000,
"xdx.jelly/xgcl/gmpki/sxf": 0x02000000,
"xdx.jelly/xgcl/gmpki/x509v3": 0x02001000,
"xdx.jelly/xgcl/gmpki/ibc": 0x02002000,
"xdx.jelly/xgcl/gmpki/gssl": 0x02003000,
"xdx.jelly/xgcl/gmpki/certless/gmcertless": 0x02004000,
+23
View File
@@ -0,0 +1,23 @@
Copyright (c) 2015, Dave Cheney <dave@cheney.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+59
View File
@@ -0,0 +1,59 @@
# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors) [![Sourcegraph](https://sourcegraph.com/github.com/pkg/errors/-/badge.svg)](https://sourcegraph.com/github.com/pkg/errors?badge)
Package errors provides simple error handling primitives.
`go get github.com/pkg/errors`
The traditional error handling idiom in Go is roughly akin to
```go
if err != nil {
return err
}
```
which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.
## Adding context to an error
The errors.Wrap function returns a new error that adds context to the original error. For example
```go
_, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrap(err, "read failed")
}
```
## Retrieving the cause of an error
Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`.
```go
type causer interface {
Cause() error
}
```
`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example:
```go
switch err := errors.Cause(err).(type) {
case *MyError:
// handle specifically
default:
// unknown error
}
```
[Read the package documentation for more information](https://godoc.org/github.com/pkg/errors).
## Roadmap
With the upcoming [Go2 error proposals](https://go.googlesource.com/proposal/+/master/design/go2draft.md) this package is moving into maintenance mode. The roadmap for a 1.0 release is as follows:
- 0.9. Remove pre Go 1.9 and Go 1.10 support, address outstanding pull requests (if possible)
- 1.0. Final release.
## Contributing
Because of the Go2 errors changes, this package is not accepting proposals for new functionality. With that said, we welcome pull requests, bug fixes and issue reports.
Before sending a PR, please discuss your change by raising an issue.
## License
BSD-2-Clause
+295
View File
@@ -0,0 +1,295 @@
// Package errors provides simple error handling primitives.
//
// The traditional error handling idiom in Go is roughly akin to
//
// if err != nil {
// return err
// }
//
// which when applied recursively up the call stack results in error reports
// without context or debugging information. The errors package allows
// programmers to add context to the failure path in their code in a way
// that does not destroy the original value of the error.
//
// # Adding context to an error
//
// The errors.Wrap function returns a new error that adds context to the
// original error by recording a stack trace at the point Wrap is called,
// together with the supplied message. For example
//
// _, err := ioutil.ReadAll(r)
// if err != nil {
// return errors.Wrap(err, "read failed")
// }
//
// If additional control is required, the errors.WithStack and
// errors.WithMessage functions destructure errors.Wrap into its component
// operations: annotating an error with a stack trace and with a message,
// respectively.
//
// # Retrieving the cause of an error
//
// Using errors.Wrap constructs a stack of errors, adding context to the
// preceding error. Depending on the nature of the error it may be necessary
// to reverse the operation of errors.Wrap to retrieve the original error
// for inspection. Any error value which implements this interface
//
// type causer interface {
// Cause() error
// }
//
// can be inspected by errors.Cause. errors.Cause will recursively retrieve
// the topmost error that does not implement causer, which is assumed to be
// the original cause. For example:
//
// switch err := errors.Cause(err).(type) {
// case *MyError:
// // handle specifically
// default:
// // unknown error
// }
//
// Although the causer interface is not exported by this package, it is
// considered a part of its stable public interface.
//
// # Formatted printing of errors
//
// All error values returned from this package implement fmt.Formatter and can
// be formatted by the fmt package. The following verbs are supported:
//
// %s print the error. If the error has a Cause it will be
// printed recursively.
// %v see %s
// %+v extended format. Each Frame of the error's StackTrace will
// be printed in detail.
//
// # Retrieving the stack trace of an error or wrapper
//
// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
// invoked. This information can be retrieved with the following interface:
//
// type stackTracer interface {
// StackTrace() errors.StackTrace
// }
//
// The returned errors.StackTrace type is defined as
//
// type StackTrace []Frame
//
// The Frame type represents a call site in the stack trace. Frame supports
// the fmt.Formatter interface that can be used for printing information about
// the stack trace of this error. For example:
//
// if err, ok := err.(stackTracer); ok {
// for _, f := range err.StackTrace() {
// fmt.Printf("%+s:%d\n", f, f)
// }
// }
//
// Although the stackTracer interface is not exported by this package, it is
// considered a part of its stable public interface.
//
// See the documentation for Frame.Format for more details.
package errors
import (
"fmt"
"io"
)
// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(message string) error {
return &fundamental{
msg: message,
stack: callers(),
}
}
// Errorf formats according to a format specifier and returns the string
// as a value that satisfies error.
// Errorf also records the stack trace at the point it was called.
func Errorf(format string, args ...interface{}) error {
return &fundamental{
msg: fmt.Sprintf(format, args...),
stack: callers(),
}
}
// fundamental is an error that has a message and a stack, but no caller.
type fundamental struct {
msg string
*stack
}
func (f *fundamental) Error() string { return f.msg }
func (f *fundamental) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, f.msg)
f.stack.Format(s, verb)
return
}
fallthrough
case 's':
io.WriteString(s, f.msg)
case 'q':
fmt.Fprintf(s, "%q", f.msg)
}
}
// WithStack annotates err with a stack trace at the point WithStack was called.
// If err is nil, WithStack returns nil.
func WithStack(err error) error {
if err == nil {
return nil
}
return &withStack{
err,
callers(),
}
}
type withStack struct {
error
*stack
}
func (w *withStack) Cause() error { return w.error }
// Unwrap provides compatibility for Go 1.13 error chains.
func (w *withStack) Unwrap() error { return w.error }
func (w *withStack) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
if e := w.Cause(); e != nil {
fmt.Fprintf(s, "%+v", e)
}
w.stack.Format(s, verb)
return
}
fallthrough
case 's':
io.WriteString(s, w.Error())
case 'q':
fmt.Fprintf(s, "%q", w.Error())
}
}
// Wrap returns an error annotating err with a stack trace
// at the point Wrap is called, and the supplied message.
// If err is nil, Wrap returns nil.
func Wrap(err error, message string) error {
if err == nil {
return nil
}
err = &withMessage{
cause: err,
msg: message,
}
return &withStack{
err,
callers(),
}
}
// Wrapf returns an error annotating err with a stack trace
// at the point Wrapf is called, and the format specifier.
// If err is nil, Wrapf returns nil.
func Wrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
err = &withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
return &withStack{
err,
callers(),
}
}
// WithMessage annotates err with a new message.
// If err is nil, WithMessage returns nil.
func WithMessage(err error, message string) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: message,
}
}
// WithMessagef annotates err with the format specifier.
// If err is nil, WithMessagef returns nil.
func WithMessagef(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
}
type withMessage struct {
cause error
msg string
}
func (w *withMessage) Error() string { return w.msg + ", caused by " + w.cause.Error() }
func (w *withMessage) Cause() error { return w.cause }
// Unwrap provides compatibility for Go 1.13 error chains.
func (w *withMessage) Unwrap() error { return w.cause }
func (w *withMessage) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
if e := w.Cause(); e != nil {
fmt.Fprintf(s, "%+v\n", e)
}
io.WriteString(s, w.msg)
return
}
fallthrough
case 's', 'q':
io.WriteString(s, w.Error())
}
}
// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following
// interface:
//
// type causer interface {
// Cause() error
// }
//
// If the error does not implement Cause, the original error will
// be returned. If the error is nil, nil will be returned without further
// investigation.
//
// Note: We modify the returned error to nil if the err does not implement Cause.
func Cause(err error) error {
type causer interface {
Cause() error
}
for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
// return err
return nil
}
+38
View File
@@ -0,0 +1,38 @@
// +build go1.13
package errors
import (
stderrors "errors"
)
// Is reports whether any error in err's chain matches target.
//
// The chain consists of err itself followed by the sequence of errors obtained by
// repeatedly calling Unwrap.
//
// An error is considered to match a target if it is equal to that target or if
// it implements a method Is(error) bool such that Is(target) returns true.
func Is(err, target error) bool { return stderrors.Is(err, target) }
// As finds the first error in err's chain that matches target, and if so, sets
// target to that error value and returns true.
//
// The chain consists of err itself followed by the sequence of errors obtained by
// repeatedly calling Unwrap.
//
// An error matches target if the error's concrete value is assignable to the value
// pointed to by target, or if the error has a method As(interface{}) bool such that
// As(target) returns true. In the latter case, the As method is responsible for
// setting target.
//
// As will panic if target is not a non-nil pointer to either a type that implements
// error, or to any interface type. As returns false if err is nil.
func As(err error, target interface{}) bool { return stderrors.As(err, target) }
// Unwrap returns the result of calling the Unwrap method on err, if err's
// type contains an Unwrap method returning error.
// Otherwise, Unwrap returns nil.
func Unwrap(err error) error {
return stderrors.Unwrap(err)
}
+177
View File
@@ -0,0 +1,177 @@
package errors
import (
"fmt"
"io"
"path"
"runtime"
"strconv"
"strings"
)
// Frame represents a program counter inside a stack frame.
// For historical reasons if Frame is interpreted as a uintptr
// its value represents the program counter + 1.
type Frame uintptr
// pc returns the program counter for this frame;
// multiple frames may have the same PC value.
func (f Frame) pc() uintptr { return uintptr(f) - 1 }
// file returns the full path to the file that contains the
// function for this Frame's pc.
func (f Frame) file() string {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return "unknown"
}
file, _ := fn.FileLine(f.pc())
return file
}
// line returns the line number of source code of the
// function for this Frame's pc.
func (f Frame) line() int {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return 0
}
_, line := fn.FileLine(f.pc())
return line
}
// name returns the name of this function, if known.
func (f Frame) name() string {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return "unknown"
}
return fn.Name()
}
// Format formats the frame according to the fmt.Formatter interface.
//
// %s source file
// %d source line
// %n function name
// %v equivalent to %s:%d
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+s function name and path of source file relative to the compile time
// GOPATH separated by \n\t (<funcname>\n\t<path>)
// %+v equivalent to %+s:%d
func (f Frame) Format(s fmt.State, verb rune) {
switch verb {
case 's':
switch {
case s.Flag('+'):
io.WriteString(s, f.name())
io.WriteString(s, "\n\t")
io.WriteString(s, f.file())
default:
io.WriteString(s, path.Base(f.file()))
}
case 'd':
io.WriteString(s, strconv.Itoa(f.line()))
case 'n':
io.WriteString(s, funcname(f.name()))
case 'v':
f.Format(s, 's')
io.WriteString(s, ":")
f.Format(s, 'd')
}
}
// MarshalText formats a stacktrace Frame as a text string. The output is the
// same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.
func (f Frame) MarshalText() ([]byte, error) {
name := f.name()
if name == "unknown" {
return []byte(name), nil
}
return []byte(fmt.Sprintf("%s %s:%d", name, f.file(), f.line())), nil
}
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
type StackTrace []Frame
// Format formats the stack of Frames according to the fmt.Formatter interface.
//
// %s lists source files for each Frame in the stack
// %v lists the source file and line number for each Frame in the stack
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+v Prints filename, function, and line number for each Frame in the stack.
func (st StackTrace) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case s.Flag('+'):
for _, f := range st {
io.WriteString(s, "\n")
f.Format(s, verb)
}
case s.Flag('#'):
fmt.Fprintf(s, "%#v", []Frame(st))
default:
st.formatSlice(s, verb)
}
case 's':
st.formatSlice(s, verb)
}
}
// formatSlice will format this StackTrace into the given buffer as a slice of
// Frame, only valid when called with '%s' or '%v'.
func (st StackTrace) formatSlice(s fmt.State, verb rune) {
io.WriteString(s, "[")
for i, f := range st {
if i > 0 {
io.WriteString(s, " ")
}
f.Format(s, verb)
}
io.WriteString(s, "]")
}
// stack represents a stack of program counters.
type stack []uintptr
func (s *stack) Format(st fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case st.Flag('+'):
for _, pc := range *s {
f := Frame(pc)
fmt.Fprintf(st, "\n%+v", f)
}
}
}
}
func (s *stack) StackTrace() StackTrace {
f := make([]Frame, len(*s))
for i := 0; i < len(f); i++ {
f[i] = Frame((*s)[i])
}
return f
}
func callers() *stack {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
var st stack = pcs[0:n]
return &st
}
// funcname removes the path prefix component of a function's name reported by func.Name().
func funcname(name string) string {
i := strings.LastIndex(name, "/")
name = name[i+1:]
i = strings.Index(name, ".")
return name[i+1:]
}