rterror

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2017 License: MIT Imports: 0 Imported by: 0

README

rterror

Build Status

golang package to handle runtime error

Install

$ go get github.com/suzuki-shunsuke/rterror

Example

package main

import (
	"fmt"
	"log"

	"github.com/suzuki-shunsuke/rterror"
)

func hello() (msg string, e error) { // declare named return value
	defer func() {
		p := recover()
		if p != nil {
			// assign RuntimeError to the surrounding function's return value
			e = rterror.NewRuntimeError(p, "")
		}
	}()
	var s *string
	fmt.Println(*s) // panic
	return "hello", nil
}

func main() {
	msg, err := hello()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(msg)
}

Change Log

See CHANGELOG.md.

Contributing

See CONTRIBUTING.md.

License

MIT

Documentation

Overview

Handle runtime error.

Example
package main

import (
	"fmt"
	"log"

	"github.com/suzuki-shunsuke/rterror"
)

func hello() (msg string, e error) { // declare named return value
	defer func() {
		p := recover()
		if p != nil {
			// assign RuntimeError to the surrounding function's return value
			e = rterror.NewRuntimeError(p, "")
		}
	}()
	var s *string
	fmt.Println(*s) // panic
	return "hello", nil
}

func main() {
	msg, err := hello()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(msg)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RuntimeError

type RuntimeError interface {
	Param() interface{}
	Error() string
}

Runtime Error interface

Example
msg, err := func() (msg string, e error) { // declare named return value
	defer func() {
		p := recover()
		if p != nil {
			// assign RuntimeError to the surrounding function's return value
			e = rterror.NewRuntimeError(p, "")
		}
	}()
	var s *string
	fmt.Println(*s) // panic
	return "hello", nil
}()
if err != nil {
	log.Fatal(err)
}
fmt.Println(msg)
Output:

func NewRuntimeError

func NewRuntimeError(p interface{}, msg string) RuntimeError

RuntimeError's constructor. `p` is a value given to panic. `msg` is a return value of Error(). If `msg` is a empty string ("") and `p` holds a `string`, Error() returns the underlying value. If `msg` is a empty string ("") but `p` doesn't hold a string, `Error()` returns a string "Runtime Error".

Jump to

Keyboard shortcuts

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