2025-05-05 00:00:56 +03:00
|
|
|
package stty
|
|
|
|
|
|
|
|
import(
|
|
|
|
"fmt"
|
2025-05-08 11:48:14 +03:00
|
|
|
"errors"
|
2025-05-05 00:00:56 +03:00
|
|
|
)
|
|
|
|
|
2025-05-05 01:22:01 +03:00
|
|
|
type unwraperr interface {
|
|
|
|
Unwrap() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type unwraperrs interface {
|
|
|
|
Unwrap() []error
|
|
|
|
}
|
|
|
|
|
|
|
|
func isUnwrapable(err error) bool {
|
|
|
|
_, ok1 := err.(unwraperr)
|
|
|
|
_, ok2 := err.(unwraperrs)
|
|
|
|
return ok1 || ok2
|
|
|
|
}
|
|
|
|
|
2025-05-08 11:07:19 +03:00
|
|
|
func enshureUnwrapable(err error) error {
|
|
|
|
if isUnwrapable(err) {
|
|
|
|
return err
|
|
|
|
}
|
2025-05-08 11:48:14 +03:00
|
|
|
return errors.New(err.Error())
|
2025-05-08 11:07:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2025-05-08 11:31:28 +03:00
|
|
|
fmt.Println(isUnwrapable(err), err.Error())
|
2025-05-08 11:07:19 +03:00
|
|
|
err = enshureUnwrapable(err)
|
2025-05-08 11:31:28 +03:00
|
|
|
fmt.Println(isUnwrapable(err), err.Error())
|
2025-05-08 11:07:19 +03:00
|
|
|
if index == 0 {
|
|
|
|
result = err
|
|
|
|
} else {
|
|
|
|
result = fmt.Errorf("%w: %w", result, err)
|
2025-05-08 12:16:37 +03:00
|
|
|
fmt.Println("1: ",result)
|
|
|
|
fmt.Println("2: ",err)
|
2025-05-08 11:07:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
2025-05-05 00:00:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func ERR_NOT_TTY() error {
|
|
|
|
return fmt.Errorf("File is not a tty")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ERR_CANNOT_OPEN_FILE() error {
|
|
|
|
return fmt.Errorf("Can not open file")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ERR_NO_WRITE_ACCESS() error {
|
|
|
|
return fmt.Errorf("No write access")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ERR_STTY_MISSING() error {
|
|
|
|
return fmt.Errorf("Stty is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ERR_BAUDRATE_NOT_SUPPORTED() error {
|
|
|
|
return fmt.Errorf("Selected baudrate is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ERR_SET_BAUDRATE_FAILED() error {
|
|
|
|
return fmt.Errorf("Failed to set baudrate using stty")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ERR_CHECK_BAUDRATE_FAILED() error {
|
|
|
|
return fmt.Errorf("Failed to check baudrate using stty")
|
|
|
|
}
|