From e2f234c17eb2cb14c0ab39b1673edd3cd1aac2af Mon Sep 17 00:00:00 2001 From: Ge Song Date: Tue, 7 May 2024 14:25:18 +0800 Subject: [PATCH] add cerror; --- cerrors/.golangci.yaml | 141 +++++++++++++++++++++++++++++++++++++++++ cerrors/code.go | 14 ++++ cerrors/error.go | 47 ++++++++++++++ cerrors/flag.go | 7 ++ cerrors/go.mod | 5 ++ cerrors/go.sum | 2 + cerrors/std.go | 8 +++ 7 files changed, 224 insertions(+) create mode 100644 cerrors/.golangci.yaml create mode 100644 cerrors/code.go create mode 100644 cerrors/error.go create mode 100644 cerrors/flag.go create mode 100644 cerrors/go.mod create mode 100644 cerrors/go.sum create mode 100644 cerrors/std.go diff --git a/cerrors/.golangci.yaml b/cerrors/.golangci.yaml new file mode 100644 index 0000000..d884b2f --- /dev/null +++ b/cerrors/.golangci.yaml @@ -0,0 +1,141 @@ +## 基于 golangci-lint@v1.52.2 +run: + timeout: 1m + build-tags: [ ] + skip-dirs: [ ] + skip-files: [ ] +linters: + disable-all: true + enable: + - errcheck + - gosimple + - govet + - ineffassign + - staticcheck + - typecheck + - unused + - asasalint + - asciicheck + - bidichk + - bodyclose + - containedctx + - cyclop + - dupl + - durationcheck + - errname + - errorlint + - exhaustive + - exportloopref + - funlen + - gocheckcompilerdirectives + - gochecknoinits + - goconst + - gocritic + - gocyclo + - goimports + - gomnd + - goprintffuncname + - gosec + - lll + - loggercheck + - makezero + - nakedret + - nestif + - nilnil + - noctx + - nolintlint + - prealloc + - predeclared + - promlinter + - reassign + - revive + - rowserrcheck + - stylecheck + - tenv + - testableexamples + - testpackage + - tparallel + - unconvert + - unparam + - usestdlibvars + - wastedassign + - whitespace +linters-settings: + errcheck: + check-type-assertions: true + exclude-functions: [ ] + govet: + enable-all: true + disable: [ ] + cyclop: + max-complexity: 10 + package-average: 0.0 + dupl: + threshold: 150 + exhaustive: + check: + - switch + - map + funlen: + lines: 100 + statements: 60 + gocritic: + disabled-checks: + - commentFormatting + settings: + captLocal: + paramsOnly: false + underef: + skipRecvDeref: false + gocyclo: + min-complexity: 20 + gomnd: + ignored-functions: + - os.Chmod + - os.Mkdir + - os.MkdirAll + - os.OpenFile + - os.WriteFile + - prometheus.ExponentialBuckets + - prometheus.ExponentialBucketsRange + - prometheus.LinearBuckets + lll: + line-length: 240 + nakedret: + max-func-lines: 10 + nestif: + min-complexity: 5 + predeclared: + ignore: "" + q: false + reassign: + patterns: + - ".*" + rowserrcheck: + packages: + - github.com/jmoiron/sqlx + tenv: + all: true + usestdlibvars: + time-month: true + time-layout: true + crypto-hash: true + default-rpc-path: true + os-dev-null: true + sql-isolation-level: true + tls-signature-scheme: true + constant-kind: true + syslog-priority: true +issues: + max-same-issues: 10 + exclude-rules: + - source: "//noinspection" + linters: [ gocritic ] + - path: "_test\\.go" + linters: + - bodyclose + - dupl + - funlen + - goconst + - gosec + - noctx diff --git a/cerrors/code.go b/cerrors/code.go new file mode 100644 index 0000000..7f20063 --- /dev/null +++ b/cerrors/code.go @@ -0,0 +1,14 @@ +package cerrors + +type Code string + +const ( + CodeOK Code = "ok" + CodeError Code = "error" + CodeBadRequest Code = "bad_request" + CodeInternal Code = "internal" + CodeInternalUpstream Code = "internal.upstream" + CodeUnexpect Code = "unexpect" + CodeUnexpectPanic Code = "unexpect.panic" + CodeUnknown Code = "unknown" +) diff --git a/cerrors/error.go b/cerrors/error.go new file mode 100644 index 0000000..c4be3d6 --- /dev/null +++ b/cerrors/error.go @@ -0,0 +1,47 @@ +package cerrors + +import ( + "git.blauwelle.com/go/crate/runtimehelper" +) + +const maximumFrames = 16 + +type Error struct { + Code Code + Message string + Debug string + Traceback []runtimehelper.Frame +} + +func (err *Error) Error() string { + if err.Message == "" { + return string(err.Code) + } + return string(err.Code) + ": " + err.Message +} + +func New(code Code, message string) error { + e := &Error{ + Code: code, + Message: message, + } + if FlagDebug { + e.Traceback = runtimehelper.Stack(1, maximumFrames) + } + return e +} + +func Wrap(err error, code Code, message string) error { + e2 := &Error{ + Code: code, + Message: message, + } + if e1, ok := err.(*Error); ok { //nolint:errorlint + e2.Debug = e1.Debug + e2.Traceback = e1.Traceback + } else if FlagDebug { + e2.Debug = err.Error() + e2.Traceback = runtimehelper.Stack(1, maximumFrames) + } + return e2 +} diff --git a/cerrors/flag.go b/cerrors/flag.go new file mode 100644 index 0000000..637dea6 --- /dev/null +++ b/cerrors/flag.go @@ -0,0 +1,7 @@ +package cerrors + +import ( + "os" +) + +var FlagDebug = os.Getenv("DEBUG") != "" diff --git a/cerrors/go.mod b/cerrors/go.mod new file mode 100644 index 0000000..b1f3727 --- /dev/null +++ b/cerrors/go.mod @@ -0,0 +1,5 @@ +module git.blauwelle.com/go/crate/cerrors + +go 1.21.1 + +require git.blauwelle.com/go/crate/runtimehelper v0.2.0 diff --git a/cerrors/go.sum b/cerrors/go.sum new file mode 100644 index 0000000..a413c81 --- /dev/null +++ b/cerrors/go.sum @@ -0,0 +1,2 @@ +git.blauwelle.com/go/crate/runtimehelper v0.2.0 h1:W19wipPCyFSGHOWzqtfouNJu7MDeJobP+iRM4bPiJpM= +git.blauwelle.com/go/crate/runtimehelper v0.2.0/go.mod h1:yVMA0GkO9AS7iuPmalHKeWyv9en0JWj25rY1vpTuHhk= diff --git a/cerrors/std.go b/cerrors/std.go new file mode 100644 index 0000000..7ae0065 --- /dev/null +++ b/cerrors/std.go @@ -0,0 +1,8 @@ +package cerrors + +import ( + "errors" +) + +var Is = errors.Is +var As = errors.As