errors

package module
v0.0.0-...-e12fffa Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2016 License: MIT Imports: 4 Imported by: 1

README

Errors

GoDoc Build Status Go Report Card

A robust errors type which implements the built-in error interface. It includes the following:

  • Code, an int field for integer error codes such as HTTP status
  • Meta, a []string field for high-level errors
  • Fields, a map[string]string field for named errors

It supports both JSON and XML marshaling.

Quickstart
package main

import (
    "encoding/json"
    "encoding/xml"
    "fmt"
    "log"
    "net/http"

    "github.com/aodin/errors"
)

func main() {
    err := errors.New()
    err.Code = http.StatusNotFound
    err.AddMeta("Not Found")
    err.SetField("ID", "Missing ID")

    // String
    fmt.Printf("%s\n", err.Error())
    // 404: Not Found; Missing ID (ID)

    // JSON
    jsonBytes, jsonErr := json.Marshal(err)
    if jsonErr != nil {
        log.Fatal(jsonErr)
    }
    fmt.Printf("%s\n", jsonBytes)
    // {"code":404,"meta":["Not Found"],"fields":{"ID":"Missing ID"}}

    // XML
    xmlBytes, xmlErr := xml.Marshal(err)
    if xmlErr != nil {
        log.Fatal(xmlErr)
    }
    fmt.Printf("%s\n", xmlBytes)
    // <Error><Code>404</Code><Metas><Meta>Not Found</Meta></Metas><Fields><ID>Missing ID</ID></Fields></Error>
}

Happy hacking!

aodin, 2016

Documentation

Overview

Package errors provides a robust errors type which implements the built-in error interface. It includes the following:

* Code, an int field for integer error codes such as HTTP status

* Meta, a []string field for high-level errors

* Fields, a map[string]string field for named errors

It supports both JSON and XML marshaling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Code   int               `json:"code,omitempty"`
	Meta   []string          `json:"meta"`
	Fields map[string]string `json:"fields"`
}

Error is an error structure with meta and field-specific errors

func BadRequest

func BadRequest() *Error

BadRequest creates an error with a 400 status code

func Message

func Message(msg string, args ...interface{}) *Error

Message creates a new *Error with the given message

func Meta

func Meta(code int, msg string, args ...interface{}) *Error

Meta returns an error with a pre-set meta error

func New

func New() *Error

New creates a new empty error

func (*Error) Add

func (er *Error) Add(msg string, args ...interface{})

Add is an alias for AddMeta

func (*Error) AddMeta

func (er *Error) AddMeta(msg string, args ...interface{})

AddMeta appends a meta error

func (Error) Error

func (er Error) Error() string

Error implements the error interface

func (Error) Exists

func (er Error) Exists() bool

Exists returns true if there are either meta or field errors

func (Error) InField

func (er Error) InField(field string) bool

InField returns true if the given field has an error

func (Error) IsEmpty

func (er Error) IsEmpty() bool

IsEmpty return false if there are no meta or field errors

func (Error) MarshalXML

func (er Error) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements a custom marshaler because Errors has a map

func (*Error) Set

func (er *Error) Set(field, msg string, args ...interface{})

Set is an alias for SetField

func (*Error) SetField

func (er *Error) SetField(field, msg string, args ...interface{})

SetField sets the error message for the field. Mutiple calls will overwrite previous messages for that field.

Jump to

Keyboard shortcuts

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