aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorOphestra Umiker <cat@ophivana.moe>2024-10-12 02:11:43 +0900
committerOphestra Umiker <cat@ophivana.moe>2024-10-12 02:11:43 +0900
commitd5c26ae59339695623a64afdaeb07dee7161a9fd (patch)
treed11b6c9349eea7a161b9bc098f78235dd9d9cb72
parent61b473a06fb12b9f3d6f9d7f07693313bd954509 (diff)
fortify: move error handling to separate file
Error handling here is way too monstrous due to terrible design of the internal/app package. Since rewriting internal/app will take a while, error handling is moved out of main to improve readability. Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
-rw-r--r--error.go61
-rw-r--r--main.go54
2 files changed, 64 insertions, 51 deletions
diff --git a/error.go b/error.go
new file mode 100644
index 00000000..ee48684a
--- /dev/null
+++ b/error.go
@@ -0,0 +1,61 @@
+package main
+
+import (
+ "errors"
+ "fmt"
+ "os"
+
+ "git.ophivana.moe/cat/fortify/internal/app"
+)
+
+func logWaitError(err error) {
+ var e *app.BaseError
+ if !app.AsBaseError(err, &e) {
+ fmt.Println("fortify: wait failed:", err)
+ } else {
+ // Wait only returns either *app.ProcessError or *app.StateStoreError wrapped in a *app.BaseError
+ var se *app.StateStoreError
+ if !errors.As(err, &se) {
+ // does not need special handling
+ fmt.Print("fortify: " + e.Message())
+ } else {
+ // inner error are either unwrapped store errors
+ // or joined errors returned by *appSealTx revert
+ // wrapped in *app.BaseError
+ var ej app.RevertCompoundError
+ if !errors.As(se.InnerErr, &ej) {
+ // does not require special handling
+ fmt.Print("fortify: " + e.Message())
+ } else {
+ errs := ej.Unwrap()
+
+ // every error here is wrapped in *app.BaseError
+ for _, ei := range errs {
+ var eb *app.BaseError
+ if !errors.As(ei, &eb) {
+ // unreachable
+ fmt.Println("fortify: invalid error type returned by revert:", ei)
+ } else {
+ // print inner *app.BaseError message
+ fmt.Print("fortify: " + eb.Message())
+ }
+ }
+ }
+ }
+ }
+}
+
+func logBaseError(err error, message string) {
+ var e *app.BaseError
+
+ if app.AsBaseError(err, &e) {
+ fmt.Print("fortify: " + e.Message())
+ } else {
+ fmt.Println(message, err)
+ }
+}
+
+func fatalf(format string, a ...any) {
+ fmt.Printf("fortify: "+format, a...)
+ os.Exit(1)
+}
diff --git a/main.go b/main.go
index c2c042da..662b1a46 100644
--- a/main.go
+++ b/main.go
@@ -1,7 +1,6 @@
package main
import (
- "errors"
"flag"
"fmt"
"os"
@@ -48,60 +47,13 @@ func main() {
} else if err = a.Start(); err != nil {
logBaseError(err, "fortify: cannot start app:")
} else if r, err = a.Wait(); err != nil {
- r = 1
-
- var e *app.BaseError
- if !app.AsBaseError(err, &e) {
- fmt.Println("fortify: wait failed:", err)
- } else {
- // Wait only returns either *app.ProcessError or *app.StateStoreError wrapped in a *app.BaseError
- var se *app.StateStoreError
- if !errors.As(err, &se) {
- // does not need special handling
- fmt.Print("fortify: " + e.Message())
- } else {
- // inner error are either unwrapped store errors
- // or joined errors returned by *appSealTx revert
- // wrapped in *app.BaseError
- var ej app.RevertCompoundError
- if !errors.As(se.InnerErr, &ej) {
- // does not require special handling
- fmt.Print("fortify: " + e.Message())
- } else {
- errs := ej.Unwrap()
-
- // every error here is wrapped in *app.BaseError
- for _, ei := range errs {
- var eb *app.BaseError
- if !errors.As(ei, &eb) {
- // unreachable
- fmt.Println("fortify: invalid error type returned by revert:", ei)
- } else {
- // print inner *app.BaseError message
- fmt.Print("fortify: " + eb.Message())
- }
- }
- }
- }
+ if r < 1 {
+ r = 1
}
+ logWaitError(err)
}
if err := a.WaitErr(); err != nil {
fmt.Println("fortify: inner wait failed:", err)
}
os.Exit(r)
}
-
-func logBaseError(err error, message string) {
- var e *app.BaseError
-
- if app.AsBaseError(err, &e) {
- fmt.Print("fortify: " + e.Message())
- } else {
- fmt.Println(message, err)
- }
-}
-
-func fatalf(format string, a ...any) {
- fmt.Printf("fortify: "+format, a...)
- os.Exit(1)
-}