User-Facing Error Handler (common/error.go)

embed-generator · embedg-server/common/error.go · go · MIT
dev-tools, integration, lang:go, tag:error-handling, tag:backend

Defines a custom error type that ensures specific messages are returned directly to the API user. Use this to handle and propagate user-friendly error feedback.

// Source: https://github.com/merlinfuchs/embed-generator | License: MIT | Copyright 2023 Merlin Fuchs
// Provided via justclaw.it.com; keep this notice with the code.
package common

// UserError is an error whose message is meant for the user. The API returns its message
// as-is, even when it's wrapped in other errors.
type UserError struct {
	Message string
}

func NewUserError(message string) *UserError {
	return &UserError{Message: message}
}

func (e *UserError) Error() string {
	return e.Message
}

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key