🚀 DAY 5 OF LAUNCH WEEK:Introducing Webhook Events for Alert Changes.Learn more →
Socket
Book a DemoInstallSign in
Socket

github.com/catmullet/deferror

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/catmullet/deferror

Source
Go
Version
v0.0.0-20210204221653-a8c328f7af5a
Version published
Created
Source

🦴 Deferror

Description

Simple way to automatically return errors from functions you defer.

Examples

Style 1

// returns "error coming from defer"
package main

import (
	"fmt"
	"github.com/catmullet/deferror"
)

func main() {
    if err := yourLogicHere(); err != nil {
		fmt.Println(err)
	}
}

// defer function that return error
func deferWithError() error {
    return fmt.Errorf("error coming from defer")
}

// function calling defer requires a named variable of interface error
func yourLogicHere() (err error) {
	// pass by pointer the error.
    defer deferror.As(deferWithError, &err)
    return
}

Style 2

// returns "error coming from defer"
package main

import (
	"fmt"
	"github.com/catmullet/deferror"
)

func main() {
    err, deferError := yourLogicHere()
    if err != nil {
		fmt.Println(err)
    }
    if deferError != nil {
		fmt.Println(deferError)
    }
}

// defer function that return error
func deferWithError() error {
    return fmt.Errorf("error coming from defer")
}

// function calling defer requires a named variable of interface error
// you can seperate out the error if you would like. deferror.As will never overwrite the error
// passed in if it is not nil.
func yourLogicHere() (err, deferError error) {
	// pass by pointer the error.
    defer deferror.As(deferWithError, &deferError)
    return
}

FAQs

Package last updated on 04 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts