Now can join multiple errors.
This commit is contained in:
parent
05da51cad0
commit
7efb404960
3 changed files with 36 additions and 10 deletions
34
error.go
34
error.go
|
@ -18,11 +18,37 @@ func isUnwrapable(err error) bool {
|
||||||
return ok1 || ok2
|
return ok1 || ok2
|
||||||
}
|
}
|
||||||
|
|
||||||
func errStack(err1 error, err2 error) error {
|
func enshureUnwrapable(err error) error {
|
||||||
if isUnwrapable(err2) {
|
if isUnwrapable(err) {
|
||||||
return fmt.Errorf("%w: %w", err1, err2)
|
return err
|
||||||
}
|
}
|
||||||
return fmt.Errorf("%w: %v", err1, err2)
|
return fmt.Errorf(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterNilErrors(errs []error) (notNilErrs []error) {
|
||||||
|
for _, err := range errs {
|
||||||
|
if err != nil {
|
||||||
|
notNilErrs = append(notNilErrs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return notNilErrs
|
||||||
|
}
|
||||||
|
|
||||||
|
func Join(errs ...error) (result error) {
|
||||||
|
errs = filterNilErrors(errs)
|
||||||
|
if len(errs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for index, err := range errs {
|
||||||
|
err = enshureUnwrapable(err)
|
||||||
|
if index == 0 {
|
||||||
|
result = err
|
||||||
|
} else {
|
||||||
|
result = fmt.Errorf("%w: %w", result, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func ERR_NOT_TTY() error {
|
func ERR_NOT_TTY() error {
|
||||||
|
|
4
real.go
4
real.go
|
@ -12,7 +12,7 @@ func (r *real) SetBaudRate(portPath string, baudrate int) error {
|
||||||
setBaudRateCMD := exec.Command("stty", "-F", portPath, fmt.Sprint(baudrate))
|
setBaudRateCMD := exec.Command("stty", "-F", portPath, fmt.Sprint(baudrate))
|
||||||
_, err := setBaudRateCMD.Output()
|
_, err := setBaudRateCMD.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errStack(ERR_SET_BAUDRATE_FAILED(), err)
|
return Join(ERR_SET_BAUDRATE_FAILED(), err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ func (r *real) TestBaudRate(portPath string, baudrate int) error {
|
||||||
|
|
||||||
newBaudRate, err := r.CheckBaudRate(portPath)
|
newBaudRate, err := r.CheckBaudRate(portPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errStack(ERR_CHECK_BAUDRATE_FAILED(), err)
|
return Join(ERR_CHECK_BAUDRATE_FAILED(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if newBaudRate == baudrate {
|
if newBaudRate == baudrate {
|
||||||
|
|
6
stty.go
6
stty.go
|
@ -25,7 +25,7 @@ func init() {
|
||||||
checkSttyCMD := exec.Command("stty", "--version")
|
checkSttyCMD := exec.Command("stty", "--version")
|
||||||
sttyOutput, err := checkSttyCMD.Output()
|
sttyOutput, err := checkSttyCMD.Output()
|
||||||
if err != nil || len(sttyOutput) == 0 {
|
if err != nil || len(sttyOutput) == 0 {
|
||||||
cmd = &dummy{errStack(ERR_STTY_MISSING(),err)}
|
cmd = &dummy{Join(ERR_STTY_MISSING(),err)}
|
||||||
}
|
}
|
||||||
cmd = &real{}
|
cmd = &real{}
|
||||||
}
|
}
|
||||||
|
@ -33,13 +33,13 @@ func init() {
|
||||||
func ValidateTtyFile(filepath string) error {
|
func ValidateTtyFile(filepath string) error {
|
||||||
file, err := os.Open(filepath)
|
file, err := os.Open(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errStack(ERR_CANNOT_OPEN_FILE(), fmt.Errorf(err.Error()))
|
return Join(ERR_CANNOT_OPEN_FILE(), fmt.Errorf(err.Error()))
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
err = unix.Access(filepath, unix.W_OK)
|
err = unix.Access(filepath, unix.W_OK)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errStack(ERR_NO_WRITE_ACCESS(), err)
|
return Join(ERR_NO_WRITE_ACCESS(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if term.IsTerminal(int(file.Fd())) {
|
if term.IsTerminal(int(file.Fd())) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue