Skip to content

Commit

Permalink
add messages to fatal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
olliebun committed May 21, 2023
1 parent 4ad51b3 commit 4b49243
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import (
"flag"
"fmt"
"log"
"net/url"
"os"

"github.com/skip2/go-qrcode"
)

func main() {
log.SetFlags(0)
f := parseFlags()
checkURL(f.url)
png, err := qrcode.Encode(f.url, qrcode.RecoveryLevel(f.recoveryLevel), int(f.size))
exitOnErr(err)
exitOnErr(err, "encode URL")
_, err = os.Stdout.Write(png)
exitOnErr(err)
exitOnErr(err, "write image to output")
}

type Flags struct {
Expand Down Expand Up @@ -73,8 +76,16 @@ func (r recoveryLevel) String() string {
return s
}

func exitOnErr(err error) {
// checkURL logs a warning if val does not look like a value HTTP request URI.
func checkURL(val string) {
_, err := url.ParseRequestURI(val)
if err != nil {
log.Fatal(err)
log.Printf("warning: %q is not a valid URL: %s", val, err)
}
}

func exitOnErr(err error, message string) {
if err != nil {
log.Fatal(fmt.Errorf("%s: %w", message, err))
}
}

0 comments on commit 4b49243

Please sign in to comment.