Skip to content

Commit

Permalink
feat: pretty errors
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Jan 18, 2024
1 parent 72c5896 commit e87b985
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var ErrorPadding = lipgloss.NewStyle().Padding(1, 2)
var ErrorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).SetString("ERROR")
var ErrorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575"))

func fatal(title string, err error) {
func printFatalError(title string, err error) {
fmt.Printf("%s", ErrorPadding.Render(ErrorHeader.String(), title))
fmt.Printf("%s\n", ErrorPadding.Render(ErrorDetails.Render(err.Error())))
os.Exit(1)
Expand Down
39 changes: 25 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ func main() {
config Config
)

_ = kong.Parse(&config,
kong.Help(helpPrinter))
k, err := kong.New(&config, kong.Help(helpPrinter))
if err != nil {
printFatalError("Freeze Error", err)
}
ctx, err := k.Parse(os.Args[1:])
if err != nil || ctx.Error != nil {
printFatalError("Invalid Usage", err)
}

c, err := configs.Open("configurations/" + config.Config + ".json")
if err != nil {
Expand All @@ -40,16 +46,21 @@ func main() {

r, err := kong.JSON(c)
if err != nil {
fatal("Invalid JSON", err)
printFatalError("Invalid JSON", err)
}
k, err = kong.New(&config, kong.Help(helpPrinter), kong.Resolvers(r))
if err != nil {
printFatalError("Something went wrong", err)
}
ctx, err = k.Parse(os.Args[1:])
if err != nil {
printFatalError("Invalid Usage", err)
}
ctx := kong.Parse(&config,
kong.Resolvers(r),
kong.Help(helpPrinter))

config.Margin = expandMargin(config.Margin)
config.Padding = expandPadding(config.Padding)

if config.Input == "" && !in.IsPipe(os.Stdin) {
if config.Input == "" && !in.IsPipe(os.Stdin) && len(ctx.Flags()) <= 0 {
_ = helpPrinter(kong.HelpOptions{}, ctx)
os.Exit(0)
}
Expand All @@ -60,7 +71,7 @@ func main() {
} else {
input, err = in.ReadFile(config.Input)
if err != nil {
fatal("File not found", err)
printFatalError("File not found", err)
}
lexer = lexers.Get(config.Input)
}
Expand All @@ -70,13 +81,13 @@ func main() {
}

if lexer == nil {
fatal("Language Unknown", errors.New("specify a language with the --language flag"))
printFatalError("Language Unknown", errors.New("specify a language with the --language flag"))
}

input = strings.TrimSpace(input)

if err != nil || input == "" {
fatal("No input", err)
printFatalError("No input", err)
}

// Format code source.
Expand All @@ -85,7 +96,7 @@ func main() {
f := formatter.New(ff, formatter.FontFamily(config.Font.Family))
it, err := l.Tokenise(nil, input)
if err != nil {
fatal("Malformed text", err)
printFatalError("Malformed text", err)
}
buf := &bytes.Buffer{}

Expand All @@ -103,12 +114,12 @@ func main() {
doc := etree.NewDocument()
_, err = doc.ReadFrom(buf)
if err != nil {
fatal("Bad SVG", err)
printFatalError("Bad SVG", err)
}

elements := doc.ChildElements()
if len(elements) < 1 {
fatal("Bad Output", nil)
printFatalError("Bad Output", nil)
}

image := elements[0]
Expand Down Expand Up @@ -172,6 +183,6 @@ func main() {
}
}
if err != nil {
fatal("Unable to write output", err)
printFatalError("Unable to write output", err)
}
}

0 comments on commit e87b985

Please sign in to comment.