Errors: Use errata to generate errors from HCL files
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
SRCS=$(wildcard *.hcl)
|
||||
OBJS=$(SRCS:.hcl=.gen.go)
|
||||
|
||||
all: $(OBJS)
|
||||
|
||||
%.gen.go: %.hcl errutil.tmpl
|
||||
eish generate --source=$< --template=./errutil.tmpl --package=errs > $@
|
||||
@@ -0,0 +1,39 @@
|
||||
// Code generated by Errata. DO NOT EDIT.
|
||||
// Errata Schema Version: {{ SchemaVersion }}
|
||||
// Grafana errors schema version: 1
|
||||
// Hash: {{ Hash }}
|
||||
package {{ Package }}
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
)
|
||||
|
||||
var (
|
||||
{%- for code, error in Errors sorted %}
|
||||
Err{{ code | constantize }} = errutil.NewBase(
|
||||
{%- if error.Cause %}
|
||||
errutil.Status{{ error.Cause }},
|
||||
{%- else %}
|
||||
errutil.StatusMissing, // Fixme: Set 'cause' to one of the statuses defined as "errutil.Status*".
|
||||
{%- endif %}
|
||||
{{ code | stringformat:"%q" }},
|
||||
{%- if error.Message && !error.Labels.template %}
|
||||
errutil.WithPublicMessage(`{{ error.Message | escape_backtick -}}`),
|
||||
{%- endif %}
|
||||
{%- if error.Labels.severity %}
|
||||
errutil.WithLogLevel(errutil.Level{{ error.Labels.severity | capfirst }}),
|
||||
{%- endif %}
|
||||
{%- if error.Guide %}
|
||||
errutil.WithGuide(`{{ error.Guide | escape_backtick -}}`),
|
||||
{%- endif %}
|
||||
){%- if error.Labels.template %}.MustTemplate(
|
||||
{%- if error.Labels.logMessage %}
|
||||
`{{ error.Labels.logMessage | escape_backtick -}}`,
|
||||
errutil.WithPublic(`{{ error.Message | escape_backtick -}}`),
|
||||
{%- else %}
|
||||
`{{ error.Message | escape_backtick -}}`,
|
||||
errutil.WithPublicFromLog(),
|
||||
{%- endif %}
|
||||
){%- endif %}
|
||||
{%- endfor %}
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
package errs
|
||||
|
||||
//go:generate make all
|
||||
@@ -15,6 +15,7 @@ type Base struct {
|
||||
messageID string
|
||||
publicMessage string
|
||||
logLevel LogLevel
|
||||
guide string
|
||||
}
|
||||
|
||||
// NewBase initializes a [Base] that is used to construct [Error].
|
||||
@@ -54,6 +55,17 @@ func WithLogLevel(lvl LogLevel) BaseOpt {
|
||||
}
|
||||
}
|
||||
|
||||
// WithGuide adds a longer text intended to support a user to resolve
|
||||
// the error.
|
||||
//
|
||||
// Used as a functional option to [NewBase].
|
||||
func WithGuide(guide string) BaseOpt {
|
||||
return func(b Base) Base {
|
||||
b.guide = guide
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
// WithPublicMessage sets the default public message that will be used
|
||||
// for errors based on this [Base].
|
||||
//
|
||||
@@ -171,6 +183,9 @@ type Error struct {
|
||||
PublicPayload map[string]interface{}
|
||||
// LogLevel provides a suggested level of logging for the error.
|
||||
LogLevel LogLevel
|
||||
// Guide is a longer text intended to support a user to resolve
|
||||
// the error.
|
||||
Guide string
|
||||
}
|
||||
|
||||
// MarshalJSON returns an error, we do not want raw [Error]s being
|
||||
@@ -229,6 +244,7 @@ type PublicError struct {
|
||||
MessageID string `json:"messageId"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Extra map[string]interface{} `json:"extra,omitempty"`
|
||||
Guide string `json:"guide,omitempty"`
|
||||
}
|
||||
|
||||
// Public returns a subset of the error with non-sensitive information
|
||||
@@ -249,5 +265,6 @@ func (e Error) Public() PublicError {
|
||||
MessageID: e.MessageID,
|
||||
Message: message,
|
||||
Extra: e.PublicPayload,
|
||||
Guide: e.Guide,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user