Categorygithub.com/knoebber/usererror
modulepackage
0.1.0
Repository: https://github.com/knoebber/usererror.git
Documentation: pkg.go.dev

# README

Usererror

Simple package for returning errors when the user makes a mistake.

Example usage for setting http response

import "github.com/knoebber/usererror"

func createUser(username string) error {
   if userExists(username) {
       return usererror.Format("username %q already exists", username)
   }

   return createUser(username)
}

func handleError(w http.ResponseWriter, err error) {
	if uErr := usererror.Convert(err); uErr != nil {
       // Set status 200 and a message in the response
       setMessage(w, uErr.Message)
    } else {
       // Set status 500
	   setInternalError(w, err)
    }
}

# Functions

Convert attempts to convert err to a usererror.
Format returns a formatted usererror.
New returns a new usererror.

# Structs

Error returns a message to the user.