maperr

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: May 8, 2020 License: MIT Imports: 6 Imported by: 1

README

maperr

install

go get github.com/podhmo/maperr

how to use

package main

import (
	"bytes"
	"encoding/json"
	"log"

	"github.com/podhmo/maperr"
)

type Person struct {
	Name string `json:"name"` // required
	Age  int    `json:"age"`
}

func (p *Person) UnmarshalJSON(b []byte) error {
	var err *maperr.Error
	var inner struct {
		Name *string `json:"name"`
		Age  *int    `json:"age"`
	}

	if rawerr := json.Unmarshal(b, &inner); rawerr != nil {
		err = err.AddSummary(rawerr.Error())
	}

	if inner.Name != nil {
		p.Name = *inner.Name
	} else {
		err = err.Add("name", maperr.Message{Text: "required"})
	}
	if inner.Age != nil {
		p.Age = *inner.Age
	}

	return err.Untyped()
}

func main() {
	b := bytes.NewBufferString(`{"age": 20}`)
	decoder := json.NewDecoder(b)

	var p Person

	if err := decoder.Decode(&p); err != nil {
		log.Fatalf("%+v", err)
	}
}

result (verbose output)

2020/05/03 17:16:34 Error -- {
  "summary": "name, required",
  "messages": {
    "name": [
      {
        "text": "required"
      }
    ]
  }
}
exit status 1

short version

If your code is something like following

  • panic(err)
  • log.Fatalf("%v", err)

Output is here.

Error -- "name, required" (1 number of errors)

Documentation

Index

Examples

Constants

View Source
const (
	// PriorityHigh is high priority
	PriorityHigh Priority = 10
	// PriorityNormal is normal priority
	PriorityNormal = 0
	// PriorityLow is low priority
	PriorityLow = -10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Summary     string               `json:"summary"`
	Messages    map[string][]Message `json:"messages,omitempty"`
	MaxPriority Priority             `json:"-"`
	Total       int                  `json:"-"`
}

Error ...

func (*Error) Add

func (e *Error) Add(name string, message Message) *Error

Add ...

func (*Error) AddSummary

func (e *Error) AddSummary(summary string) *Error

AddSummary ...

func (*Error) Error

func (e *Error) Error() string

Error ...

func (*Error) Format

func (e *Error) Format(f fmt.State, c rune)

Format ...

Example
package main

import (
	"fmt"

	"github.com/podhmo/maperr"
)

func main() {
	var err *maperr.Error
	err = err.
		AddSummary("💣error is occured").
		Add("name", maperr.Message{Text: "name is empty"})

	fmt.Printf("%v\n", err)
	fmt.Printf("%+v\n", err)

}
Output:

Error -- "💣error is occured" (1 number of errors)
Error -- {
  "summary": "💣error is occured",
  "messages": {
    "name": [
      {
        "text": "name is empty"
      }
    ]
  }
}

func (*Error) MarshalJSON added in v0.1.0

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (*Error) Untyped

func (e *Error) Untyped() error

Untyped ...

type FlattenLayout added in v0.1.0

type FlattenLayout struct {
	Summary     string               `json:"summary"`
	Messages    map[string][]Message `json:"messages,omitempty"`
	MaxPriority Priority             `json:"-"`
	Total       int                  `json:"-"`
}

FlattenLayout ...

func (*FlattenLayout) Layout added in v0.1.0

func (v *FlattenLayout) Layout(err *Error) interface{}

Layout ...

type FullLayout added in v0.1.0

type FullLayout struct {
	Summary     string               `json:"summary"`
	Messages    map[string][]Message `json:"messages,omitempty"`
	MaxPriority Priority             `json:"-"`
	Total       int                  `json:"-"`
}

FullLayout ...

func (*FullLayout) Layout added in v0.1.0

func (v *FullLayout) Layout(err *Error) interface{}

Layout ...

type Layout added in v0.1.0

type Layout interface {
	Layout(*Error) interface{}
}

Layout ...

var DefaultLayout Layout

type Message

type Message struct {
	Text     string      `json:"text,omitempty"`
	Error    interface{} `json:"error,omitempty"`
	Priority Priority    `json:"-"`
	Kind     string      `json:"-"`
}

Message ...

type Priority

type Priority int

Priority ...

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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