Skip to content
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

all: purge opaque errors from suite #24

Open
kortschak opened this issue Sep 18, 2016 · 1 comment
Open

all: purge opaque errors from suite #24

kortschak opened this issue Sep 18, 2016 · 1 comment

Comments

@kortschak
Copy link
Member

kortschak commented Sep 18, 2016

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
}
@kortschak
Copy link
Member Author

kortschak commented Apr 11, 2017

Use of fmt.Errorf in:

  • led.go
  • buttons.go
  • ev3dev.go
  • lego_port.go
  • sensor.go
  • speaker.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant