Skip to content

Commit

Permalink
Implement -errorStackTrace opt-in option (#41)
Browse files Browse the repository at this point in the history
The stack trace is reflect-compatible with:
- github.com/pkg/errors
- golang.org/x/xerrors
- golang.org/x/exp/errors

and thus can be inspected via reflect pkg by tools like:
- https://github.com/getsentry/sentry-go
- https://github.com/golang-cz/devslog
- etc.
  • Loading branch information
VojtechVitek authored Sep 27, 2023
1 parent c187529 commit f3e0a7e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Change any of the following values by passing `-option="Value"` CLI flag to `web
| `-types=false` | `true` | don't generate types | v0.13.0 |
| `-json=jsoniter` | `"stdlib"` | use alternative json encoding package | v0.12.0 |
| `-fixEmptyArrays` | `false` | force empty array `[]` instead of `null` in JSON (see Go [#27589][go27589]) | v0.13.0 |
| `-errorStackTrace` | `false` | enables error stack traces | v0.14.0 |
| `-legacyErrors` | `false` | enable legacy errors (v0.10.0 or older) | v0.11.0 |

Example:
Expand Down
14 changes: 14 additions & 0 deletions errors.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type WebRPCError struct {
Cause string `json:"cause,omitempty"`
HTTPStatus int `json:"status"`
cause error

{{- if $opts.errorStackTrace }}
{{- /* The below field is reflect-compatible with golang.org/x/xerrors.*/}}
frame struct { frames [3]uintptr }
{{- end}}
}

var _ error = WebRPCError{}
Expand Down Expand Up @@ -44,9 +49,18 @@ func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError {
err := rpcErr
err.cause = cause
err.Cause = cause.Error()
{{- if $opts.errorStackTrace }}
runtime.Callers(1, err.frame.frames[:])
{{- end}}
return err
}

{{ if $opts.errorStackTrace -}}
func (e WebRPCError) StackFrames() []uintptr {
return e.frame.frames[:]
}
{{- end }}

// Webrpc errors
var (
{{- range $_, $error := $webrpcErrors}}
Expand Down
4 changes: 4 additions & 0 deletions imports.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
{{- set $stdlibImports "encoding/json" "" -}}
{{- end -}}

{{- if $opts.errorStackTrace }}
{{- set $stdlibImports "runtime" "" -}}
{{- end -}}

{{- if $opts.client }}
{{- set $stdlibImports "bytes" "" -}}
{{- set $stdlibImports "io" "" -}}
Expand Down
1 change: 1 addition & 0 deletions main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{{- set $opts "json" (default .Opts.json "stdlib") -}}
{{- set $opts "importTypesFrom" (default .Opts.importTypesFrom "" ) -}}
{{- set $opts "fixEmptyArrays" (ternary (in .Opts.fixEmptyArrays "" "true") true false) -}}
{{- set $opts "errorStackTrace" (ternary (in .Opts.errorStackTrace "" "true") true false) -}}
{{- set $opts "legacyErrors" (ternary (in .Opts.legacyErrors "" "true") true false) -}}

{{- $typePrefix := (last (split "/" $opts.importTypesFrom)) -}}
Expand Down

0 comments on commit f3e0a7e

Please sign in to comment.