refactor(netxlite/errorsx): extract string-suffix classifier (#482)

This change makes the code more tidy and easier to read.

No functional change, though.

See https://github.com/ooni/probe/issues/1591.
This commit is contained in:
Simone Basso 2021-09-07 21:18:26 +02:00 committed by GitHub
parent a56b284b0e
commit b7786a7324
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 18 deletions

View File

@ -11,11 +11,6 @@ import (
"github.com/ooni/probe-cli/v3/internal/scrubber"
)
// TODO (kelmenhorst, bassosimone):
// Use errors.Is / errors.As more often, when possible, in this classifier.
// These methods are more robust to library changes than strings.
// errors.Is / errors.As can only be used when the error is exported.
// ClassifyGenericError is the generic classifier mapping an error
// occurred during an operation to an OONI failure string.
//
@ -58,6 +53,18 @@ func ClassifyGenericError(err error) string {
return FailureInterrupted
}
if failure := classifyWithStringSuffix(err); failure != "" {
return failure
}
formatted := fmt.Sprintf("unknown_failure: %s", err.Error())
return scrubber.Scrub(formatted) // scrub IP addresses in the error
}
// classifyWithStringSuffix is a subset of ClassifyGenericError that
// performs classification by looking at error suffixes. This function
// will return an empty string if it cannot classify the error.
func classifyWithStringSuffix(err error) string {
s := err.Error()
if strings.HasSuffix(s, "operation was canceled") {
return FailureInterrupted
@ -83,9 +90,7 @@ func ClassifyGenericError(err error) string {
// that we return here is significantly more specific.
return FailureDNSNXDOMAINError
}
formatted := fmt.Sprintf("unknown_failure: %s", s)
return scrubber.Scrub(formatted) // scrub IP addresses in the error
return "" // not found
}
// TLS alert protocol as defined in RFC8446. We need these definitions

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 20:26:06.502417 +0200 CEST m=+0.133209876
// Generated: 2021-09-07 20:42:20.172357 +0200 CEST m=+0.135433792
package errorsx
@ -64,9 +64,6 @@ const (
// proper OONI error. Returns the OONI error string
// on success, an empty string otherwise.
func classifySyscallError(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 ""

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 20:26:06.547786 +0200 CEST m=+0.178579584
// Generated: 2021-09-07 20:42:20.217324 +0200 CEST m=+0.180405501
package errorsx

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 20:26:06.370246 +0200 CEST m=+0.001036417
// Generated: 2021-09-07 20:42:20.037485 +0200 CEST m=+0.000558667
package errorsx

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 20:26:06.478577 +0200 CEST m=+0.109369751
// Generated: 2021-09-07 20:42:20.147482 +0200 CEST m=+0.110557751
package errorsx

View File

@ -198,9 +198,6 @@ func writeGenericFile() {
fileWrite(filep, "// proper OONI error. Returns the OONI error string\n")
fileWrite(filep, "// on success, an empty string otherwise.\n")
fileWrite(filep, "func classifySyscallError(err error) string {\n")
fileWrite(filep, "\t// filter out system errors: necessary to detect all windows errors\n")
fileWrite(filep, "\t// https://github.com/ooni/probe/issues/1526 describes the problem\n")
fileWrite(filep, "\t// of mapping localized windows errors.\n")
fileWrite(filep, "\tvar errno syscall.Errno\n")
fileWrite(filep, "\tif !errors.As(err, &errno) {\n")
fileWrite(filep, "\t\treturn \"\"\n")