Skip to content

all: purge opaque errors from suite #24

@kortschak

Description

@kortschak

The ev3go/ev3dev package was easy to start with errors.New and fmt.Errorf, but it makes mechanical error handling difficult. So add three (?) error interface types and relevant implementations:

Interfaces

type ValidValuer interface {
    ValidValues() []string
}
type ValidRanger interface {
    ValidRange() (min, max int)
}
type ValidDurationRanger interface {
    ValidDurationRange() (min, max time.Duration)
}

Implementation example

type NegativeDuration time.Duration

func (d NegativeDuration) Error() string {
    if d >= 0 {
        panic(fmt.Sprintf("ev3dev: invalid negative duration: %v", d))
    }
    return fmt.Sprintf("ev3dev: invalid duration: %v (must be positive)", time.Duration(d))
}

func (d NegativeDuration) ValidDurationRange() (min, max time.Duration) {
    if d >= 0 {
        panic(fmt.Sprintf("ev3dev: invalid negative duration: %v", d))
    }
    return 0, MaxInt64
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions