From bc31a523fabd21c87eda8bfeff803daa16c14156 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 18 Nov 2017 00:12:56 +0900 Subject: [PATCH] Catch errors in main function --- examples/error.feature | 5 +++++ main.go | 10 ++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 examples/error.feature diff --git a/examples/error.feature b/examples/error.feature new file mode 100644 index 0000000..0763c47 --- /dev/null +++ b/examples/error.feature @@ -0,0 +1,5 @@ +Feature: Error + Scenario: Fail with no argument + Given a file named "foo.md" with "" + When I run `linkcheck` + Then the exit status should be 1 diff --git a/main.go b/main.go index f6a3f09..fc086d7 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,13 @@ import ( ) func main() { + defer func() { + if r := recover(); r != nil { + fmt.Fprintln(os.Stderr, r.(error).Error()) + os.Exit(1) + } + }() + getArgs() } @@ -20,8 +27,7 @@ Usage: args, err := docopt.Parse(usage, nil, true, "linkcheck", true) if err != nil { - fmt.Fprintln(os.Stderr, err.Error()) - os.Exit(1) + panic(err) } return args