errors

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2023 License: MIT Imports: 5 Imported by: 3

README

GitHub tag Nix Devshell Continuous Integration License Language Go Report Card

go-fyi

Explore the docs »

error.fyi's Golang client library. It is used to wrap errors and add extra context when the error is returned.

Installation · Get Started · Reference · Report Bug


Warning The project is not production ready.

Motivation

When developing a software application error messages tend to be an afterthought or forgotten all together, which in turn might make for a poor user experience when troubleshooting errors. With error.fyi I hope to improve the story around error messaging and troubleshooting.

An error wrapped with go-fyi should always be able to answer the following:

  • What caused the issue?
  • How they could solve the issue.
  • What to do to solve the issue.

Usage

Note error.fyi CLI is required to generate the error.yaml from comments. Alternatively you can manually create your error.yaml.

See Get Started for more information on generating the error.yaml.

package main

import (
  "errors"
  "log"
  _"embed"

  fyi "github.com/error-fyi/go-fyi"
)

//go:embed errors.yaml
var errorsYAML []byte

// @fyi name example-app
// @fyi title Example App
// @fyi base_url docs.example.com
// @fyi version v0.1.0
func main() {
  fyi.SetManifest(errorsYAML)
  err := doSomething()
  if err != nil {
    log.Fatal(err)
  }
}

func doSomething() error {
  // TODO improve this error message
  err := errors.New("something went wrong, please try again")
  // @fyi.error code main_error_18
  // @fyi.error title Transaction Error
  // @fyi.error short There was an error during transaction, value exceeded limit
  // @fyi.error severity low
  // @fyi.error.suggestion short Retry the transaction with a lower input value
  return fyi.Error(err, "main_error_18")
}

Output:

$ go run main.go
2023/10/03 22:46:12 [something went wrong, please try again]

   TRANSACTION ERROR                                                          
                                                                              
  ## What caused the error                                                    
                                                                              
  There was an error during transaction, value exceeded limit                 
                                                                              
  ## Quick Solutions                                                          
                                                                              
  │ Additional information is available at: docs.example.com/example-          
  │ app/errors/main_error_18                                                   
                                                                              
  • Suggestion: Retry the transaction with a lower input value                

exit status 1
example manifest
---
# Code generated by fyictl: https://github.com/tfadeyi/errors.
# DO NOT EDIT.
base_url: docs.example.com
errors_definitions:
    main_error_18:
        code: main_error_18
        meta:
            loc:
                filename: main.go
                line: 24
        severity: low
        short: There was an error during transaction, value exceeded limit
        suggestions:
            "1":
                error_code: main_error_18
                id: "1"
                short: Retry the transaction with a lower input value
        title: Transaction Error
name: example-app
title: Example App
version: v0.1.0

Installation

go get -u github.com/error-fyi/go-fyi

Development

Development is entirely done using Nix devshell, as such Nix should be present in the development machine.

$ source env-dev.sh
$ develop

This will boot up a Nix devshell with the need tools and information.

Contributing

Everyone is welcome to contribute to the project.

Please see CONTRIBUTING.md for information on how to get started.

Feedback is always appreciated, whether it's a bug or feature request, feel free to open an issue using one of the templates.

License

MIT, see LICENSE.md.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(err error, code string, opts ...HandlerOption) error

Error wraps the incoming error with error defined by the application error manifest according to the input code. if no error is found in the application error manifest, the original error is returned.

func ErrorWithContext

func ErrorWithContext(ctx context.Context, err error, code string, opts ...HandlerOption) error

ErrorWithContext wraps the incoming error with error defined by the application error manifest according to the input code. if no error is found in the application error manifest, the original error is returned.

func SetDisplayErrorURL

func SetDisplayErrorURL(flag bool)

SetDisplayErrorURL

func SetDisplayedSuggestions

func SetDisplayedSuggestions(num int)

func SetErrorParentPath

func SetErrorParentPath(parentDir string)

func SetLogger

func SetLogger(logger *log.Logger)

func SetManifest

func SetManifest(content []byte)

func SetManifestFilename

func SetManifestFilename(filepath string)

func SetMarkdownRender

func SetMarkdownRender(markdown bool)

func SetOverrideErrorURL

func SetOverrideErrorURL(url string)

SetOverrideErrorURL

func SetShowShortSummary

func SetShowShortSummary(flag bool)

SetShowShortSummary

func SetSilence

func SetSilence(silence bool)

Types

type Handler

type Handler struct {
	Options *HandlerOptions
}

Handler is the wrapper struct for errors

func New

func New(opts ...HandlerOption) *Handler

New creates and configures a new instance of the error handler.

example:

errHandler := New(WithManifest("content"),WithNoLogger(),WithRenderMarkdown(true))

errHandler.Error(err, "error code")

func (*Handler) Error

func (w *Handler) Error(err error, code string, opts ...HandlerOption) error

Error wraps the incoming error with error defined by the application error manifest according to the input code. if no error is found in the application error manifest, the original error is returned.

func (*Handler) ErrorWithContext

func (w *Handler) ErrorWithContext(ctx context.Context, err error, code string, opts ...HandlerOption) error

ErrorWithContext wraps the incoming error with error defined by the Aloe specification according to the input code. if no error is found in the specification, the original error is returned.

func (*Handler) SetDisplayErrorURL

func (w *Handler) SetDisplayErrorURL(flag bool)

SetDisplayErrorURL

func (*Handler) SetDisplayedSuggestions

func (w *Handler) SetDisplayedSuggestions(num int)

SetDisplayedSuggestions

func (*Handler) SetErrorParentPath

func (w *Handler) SetErrorParentPath(parentDir string)

SetErrorParentPath

func (*Handler) SetLogger

func (w *Handler) SetLogger(logger *log.Logger)

SetLogger sets the logger used by the Handler internally. note: pass nil if no logging is wanted

func (*Handler) SetManifest

func (w *Handler) SetManifest(content []byte)

SetManifest sets the source manifest used by the Handler note: it is not required if the SetManifestFilename is set

func (*Handler) SetManifestFilename

func (w *Handler) SetManifestFilename(filepath string)

SetManifestFilename sets the file path of the manifest used by the Handler. note: it is not required if SetManifest is set

func (*Handler) SetMarkdownRender

func (w *Handler) SetMarkdownRender(markdown bool)

SetMarkdownRender

func (*Handler) SetOverrideErrorURL

func (w *Handler) SetOverrideErrorURL(url string)

SetOverrideErrorURL

func (*Handler) SetShowShortSummary

func (w *Handler) SetShowShortSummary(flag bool)

SetShowShortSummary

func (*Handler) SetSilence

func (w *Handler) SetSilence(silence bool)

SetSilence

type HandlerOption

type HandlerOption func(o *HandlerOptions)

HandlerOption is a more atomic to configure the different HandlerOptions rather than passing the entire ErrClientOptions struct.

func WithDisplayErrorURL

func WithDisplayErrorURL(flag bool) HandlerOption

func WithErrorParentPath

func WithErrorParentPath(parentDir string) HandlerOption

func WithLogger

func WithLogger(logger *log.Logger) HandlerOption

func WithManifest

func WithManifest(source []byte) HandlerOption

func WithManifestFilename

func WithManifestFilename(filename string) HandlerOption

func WithNoLogger

func WithNoLogger() HandlerOption

func WithNumberOfSuggestions

func WithNumberOfSuggestions(num int) HandlerOption

func WithOverrideErrorURL

func WithOverrideErrorURL(url string) HandlerOption

func WithRenderMarkdown

func WithRenderMarkdown(markdown bool) HandlerOption

func WithShowShortSummary

func WithShowShortSummary(flag bool) HandlerOption

func WithSilence

func WithSilence(silence bool) HandlerOption

type HandlerOptions

type HandlerOptions struct {
	// Logger is internal Logger for the wrapper, leave nil to avoid logging
	Logger *log.Logger

	// Silent globally sets the error handler to stop adding the additional error context in the input error of Error() and ErrorWithContext()
	Silent bool

	errorclient.ErrClientOptions
}

HandlerOptions contains all the different configuration values available to the wrapper

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL