Skip to content
This repository has been archived by the owner on Jun 12, 2021. It is now read-only.

Commit

Permalink
improved error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
reujab committed Jul 19, 2017
1 parent a80e477 commit fa8fd7e
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 30 deletions.
7 changes: 3 additions & 4 deletions cmdtime.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os"
"time"

Expand All @@ -12,10 +11,10 @@ func cmdTimeSegment(segment *Segment) {
duration, err := time.ParseDuration(os.Getenv("cmdtime"))
if err != nil {
if shell == "bash" {
fmt.Fprintln(os.Stderr, "bronze: The 'cmdtime' module is supported in bash.")
os.Exit(1)
dief("cmdtime: bash is not supported")
} else {
dief("cmdtime: invalid $cmdtime: %q", os.Getenv("cmdtime"))
}
panic(err)
}

threshold, err := time.ParseDuration(os.Getenv("BRONZE_CMDTIME_THRESHOLD"))
Expand Down
3 changes: 1 addition & 2 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
//go:generate go-bindata -nocompress init.bash init.zsh init.fish

import (
"fmt"
"os"

"github.com/urfave/cli"
Expand All @@ -18,7 +17,7 @@ var cmdInit = cli.Command{
if err == nil {
os.Stdout.Write(script)
} else {
fmt.Fprintln(os.Stderr, "bronze: Unrecognized shell.")
dief("unrecognized shell")
}
return nil
},
Expand Down
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"
"path/filepath"

Expand All @@ -26,3 +27,14 @@ func die(err error) {
panic(err)
}
}

func dief(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "bronze: "+format, args...)
os.Exit(1)
}

func dieIf(err error, format string, args ...interface{}) {
if err != nil {
dief(format, args...)
}
}
6 changes: 1 addition & 5 deletions modules.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package main

import (
"fmt"
"os"

. "github.com/reujab/bronze/types"
)

Expand All @@ -28,7 +25,6 @@ func handleModule(module string, segment *Segment, args []string) {
case "plugin":
pluginSegment(segment, args)
default:
fmt.Fprintf(os.Stderr, "bronze: Invalid module: %q.\n", module)
os.Exit(1)
dief("invalid module: %q", module)
}
}
2 changes: 1 addition & 1 deletion packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func packagesSegment(segment *Segment) {
conn, err := net.Dial("unix", "/tmp/packagesd.sock")
die(err)
dieIf(err, "failed to connect to packagesd socket")
defer func() { die(conn.Close()) }()

packages, err := ioutil.ReadAll(conn)
Expand Down
10 changes: 4 additions & 6 deletions plugin.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package main

import (
"fmt"
"os"
"path/filepath"
"plugin"

. "github.com/reujab/bronze/types"
)

func pluginSegment(segment *Segment, args []string) {
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "bronze: plugin: Expected at least one argument.")
os.Exit(1)
dief("plugin: expected at least one argument")
}

plug, err := plugin.Open(args[0])
die(err)
dieIf(err, "plugin: failed to open plugin: %q", args[0])

handler, err := plug.Lookup("Main")
die(err)
dieIf(err, "plugin: failed to lookup Main symbol in %q", filepath.Base(args[0]))
handler.(func(*Segment, []string))(segment, args[1:])
}
4 changes: 1 addition & 3 deletions print.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"os"
"strings"
"sync"

Expand Down Expand Up @@ -34,8 +33,7 @@ func cmdPrintAction(args []string) {
// validate argument
fields := strings.Split(arg, ":")
if len(fields) < 3 {
fmt.Fprintf(os.Stderr, "bronze: Invalid argument: %q. At least three fields expected.\n", arg)
os.Exit(1)
dief("invalid argument: %q, at least three fields expected", arg)
}

segment := &Segment{
Expand Down
13 changes: 4 additions & 9 deletions sh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"os"
)

var colors = map[string]string{
Expand Down Expand Up @@ -32,15 +31,13 @@ func escapeBackground(color string) string {
case "bash":
code, ok := colors["bg-"+color]
if !ok {
fmt.Fprintf(os.Stderr, "bronze: Invalid background color: %q.", color)
os.Exit(1)
dief("invalid background color: %q", color)
}
return "\\[\x1b[" + code + "m\\]"
default:
code, ok := colors["bg-"+color]
if !ok {
fmt.Fprintf(os.Stderr, "bronze: Invalid background color: %q.", color)
os.Exit(1)
dief("invalid background color: %q", color)
}
return "\x1b[" + code + "m"
}
Expand All @@ -53,15 +50,13 @@ func escapeForeground(color string) string {
case "bash":
code, ok := colors["fg-"+color]
if !ok {
fmt.Fprintf(os.Stderr, "bronze: Invalid foreground color: %q.", color)
os.Exit(1)
dief("invalid background color: %q", color)
}
return "\\[\x1b[" + code + "m\\]"
default:
code, ok := colors["fg-"+color]
if !ok {
fmt.Fprintf(os.Stderr, "bronze: Invalid foreground color: %q.", color)
os.Exit(1)
dief("invalid background color: %q", color)
}
return "\x1b[" + code + "m"
}
Expand Down

0 comments on commit fa8fd7e

Please sign in to comment.