aboutsummaryrefslogtreecommitdiffstats
path: root/errors.go
diff options
context:
space:
mode:
Diffstat (limited to 'errors.go')
-rw-r--r--errors.go96
1 files changed, 96 insertions, 0 deletions
diff --git a/errors.go b/errors.go
new file mode 100644
index 0000000..cb356be
--- /dev/null
+++ b/errors.go
@@ -0,0 +1,96 @@
+package envelope
+
+import (
+ "fmt"
+ "reflect"
+)
+
+// BaseError represents the base error type for the envelope package
+type BaseError struct {
+ // Message is a human-readable error description
+ Message string
+ // Err is the underlying error, if any
+ Err error
+}
+
+func (e *BaseError) Error() string {
+ if e.Err != nil {
+ return fmt.Sprintf("%s: %v", e.Message, e.Err)
+ }
+ return e.Message
+}
+
+func (e *BaseError) Unwrap() error {
+ return e.Err
+}
+
+type NotPointerToStructError struct {
+ BaseError
+ ActualType reflect.Type
+}
+
+func (e *NotPointerToStructError) Error() string {
+ return e.Message
+}
+
+func newNotPointerToStructError(t reflect.Type) *NotPointerToStructError {
+ return &NotPointerToStructError{
+ BaseError: BaseError{
+ Message: "value must be a pointer to a struct",
+ },
+ ActualType: t,
+ }
+}
+
+// UnsupportedTypeError is returned when a field has a type that cannot be parsed
+type UnsupportedTypeError struct {
+ BaseError
+ // FieldName is the name of the field with the unsupported type
+ FieldName string
+ // FieldType is the unsupported type
+ FieldType reflect.Type
+}
+
+func (e *UnsupportedTypeError) Error() string {
+ return e.Message
+}
+
+func newUnsupportedTypeError(fieldName string, fieldType reflect.Type) *UnsupportedTypeError {
+ return &UnsupportedTypeError{
+ BaseError: BaseError{
+ Message: fmt.Sprintf("unsupported field type for field %s", fieldName),
+ },
+ FieldName: fieldName,
+ FieldType: fieldType,
+ }
+}
+
+// ParseError is returned when a value cannot be parsed into the desired type
+type ParseError struct {
+ BaseError
+ // FieldName is the name of the field being parsed
+ FieldName string
+ // EnvName is the name of the environment variable
+ EnvName string
+ // Value is the string value that could not be parsed
+ Value string
+ // TargetType is the type that the value could not be parsed into
+ TargetType reflect.Type
+}
+
+func (e *ParseError) Error() string {
+ return e.Message
+}
+
+func newParseError(fieldName string, envName string, value string, targetType reflect.Type, err error) *ParseError {
+ return &ParseError{
+ BaseError: BaseError{
+ Message: fmt.Sprintf("error parsing value '%s' to '%v' for field '%s' from environment variable '%s'", value, targetType, fieldName, envName),
+ Err: err,
+ },
+ FieldName: fieldName,
+ EnvName: envName,
+ Value: value,
+ TargetType: targetType,
+ }
+}