refactor(errorsx): auto-generate syscall errors mapping (#429)

* refactor(errorsx): auto-generate syscall errors mapping

Part of https://github.com/ooni/probe/issues/1505

* fix: generate independently of the platform
This commit is contained in:
Simone Basso 2021-07-02 15:22:02 +02:00 committed by GitHub
commit 3747598b4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 221 additions and 18 deletions

37
internal/errorsx/errno.go Normal file
View file

@ -0,0 +1,37 @@
// Code generated by go generate; DO NOT EDIT.
// Generated: 2021-07-02 15:15:17.997258 +0200 CEST m=+0.110031584
package errorsx
//go:generate go run ./generator/
import (
"errors"
"syscall"
)
// toSyscallErr converts a syscall error to the
// proper OONI error. Returns the OONI error string
// on success, an empty string otherwise.
func toSyscallErr(err error) string {
// filter out system errors: necessary to detect all windows errors
// https://github.com/ooni/probe/issues/1526 describes the problem
// of mapping localized windows errors.
var errno syscall.Errno
if !errors.As(err, &errno) {
return ""
}
switch errno {
case ECANCELED:
return FailureInterrupted
case ECONNREFUSED:
return FailureConnectionRefused
case ECONNRESET:
return FailureConnectionReset
case EHOSTUNREACH:
return FailureHostUnreachable
case ETIMEDOUT:
return FailureGenericTimeoutError
}
return ""
}