Skip to content

feat: add common error interface #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add common error interface
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
  • Loading branch information
sagikazarmark committed Jun 16, 2025
commit 9661f6d07c319da00ae0508d99df5f3f0c3953bd
34 changes: 27 additions & 7 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import (
"reflect"
)

// Error interface is implemented by all errors emitted by mapstructure.
//
// Errors from underlying libraries MAY not be wrapped.
//
// Use [errors.As] to check if an error implements this interface.
type Error interface {
error

mapstructure()
}

// DecodeError is a generic error type that holds information about
// a decoding error together with the name of the field that caused the error.
type DecodeError struct {
Expand All @@ -28,30 +39,39 @@ func (e *DecodeError) Unwrap() error {
}

func (e *DecodeError) Error() string {
return fmt.Sprintf("'%s' %s", e.name, e.err)
return fmt.Sprintf("decoding '%s': %s", e.name, e.err)
}

func (*DecodeError) mapstructure() {}

// ParseError is an error type that indicates a value could not be parsed
// into the expected type.
type ParseError struct {
Expected reflect.Value
Value interface{}
Value any
Err error
}

func (e *ParseError) Error() string {
return fmt.Sprintf("cannot parse '%s' as '%s': %s",
e.Value, e.Expected.Type(), e.Err)
return fmt.Sprintf("cannot parse '%s' as '%s': %s", e.Value, e.Expected.Type(), e.Err)
}

func (*ParseError) mapstructure() {}

// UnconvertibleTypeError is an error type that indicates a value could not be
// converted to the expected type.
type UnconvertibleTypeError struct {
Expected reflect.Value
Value interface{}
Value any
}

func (e *UnconvertibleTypeError) Error() string {
return fmt.Sprintf("expected type '%s', got unconvertible type '%s', value: '%v'",
e.Expected.Type(), reflect.TypeOf(e.Value), e.Value)
return fmt.Sprintf(
"expected type '%s', got unconvertible type '%s', value: '%v'",
e.Expected.Type(),
reflect.TypeOf(e.Value),
e.Value,
)
}

func (*UnconvertibleTypeError) mapstructure() {}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy