errmsg

package
v0.6.0-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 3 Imported by: 60

README

errmsg

Add end-user messages to errors.

err.Error() doesn't usually provide a human-friendly output. errmsg allows errors to carry an (extendable) end-user message that can be used in e.g. handlers.

Here is an example on how it can be used:

package connector

import (
    // ...
    "github.com/instill-ai/x/errmsg"
)

func (c *Client) sendReq(reqURL, method, contentType string, data io.Reader) ([]byte, error) {
    // ...

    res, err := c.HTTPClient.Do(req)
    if err != nil {
        err := fmt.Errorf("failed to call connector vendor: %w", err)
        return nil, errmsg.AddMessage(err, "Failed to call Vendor API.")
    }

    if res.StatusCode < 200 || res.StatusCode >= 300 {
        err := fmt.Errorf("vendor responded with status code %d", res.StatusCode)
        msg := fmt.Sprintf("Vendor responded with a %d status code.", res.StatusCode)
        return nil, errmsg.AddMessage(err, msg)
    }

    // ...
}
package handler

func (h *PublicHandler) DoAction(ctx context.Context, req *pb.DoActionRequest) (*pb.DoActionResponse, error) {
    resp, err := h.triggerActionSteps(ctx, req)
    if err != nil {
    resp.Outputs, resp.Metadata, err = h.triggerNamespacePipeline(ctx, req)
        return nil, status.Error(asGRPCStatus(err), errmsg.MessageOrErr(err))
    }

    return resp, nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddMessage

func AddMessage(err error, msg string) error

AddMessage adds an end-user message to an error, prepending it to any potential existing message.

func Message

func Message(err error) string

Message extracts an end-user message from the error.

func MessageOrErr

func MessageOrErr(err error) string

MessageOrErr extracts an end-user message from the error. If no message is found, err.Error() is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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