reportix

package module
v0.0.0-...-0acbf73 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

Reportix

Reportix is a Golang package designed to help developers create meaningful error payloads and translate gRPC error messages to JSON via an interceptor. With Reportix, you can enhance error tracing and enable better error handling across different teams.

Features

  • Error Interceptor: Intercepts and processes errors returned by gRPC methods.
  • Error Payload Creation: Constructs detailed error payloads containing reason, domain, code, message, and metadata.
  • Error Translation: Converts gRPC errors to a JSON format, aiding frontend developers in error handling.
  • Callback Support: Allows custom error processing through callback functions.

Installation

go get github.com/problem-company-toolkit/reportix

Usage

Here's a brief example of how you can use Reportix:

import (
    "context"
    "fmt"
    "github.com/problem-company-toolkit/reportix"
    "google.golang.org/grpc/codes"
    "google.golang.org/genproto/googleapis/rpc/errdetails"
)

// Initialize error with details and metadata
err := reportix.NewError(
    codes.InvalidArgument,
    "Invalid UUID",
    &errdetails.ErrorInfo{
        Reason: "MISSING_FIELD",
        Domain: "example-service",
        Metadata: map[string]string{
            "field":  "id",
            "object": "user",
            "error":  "missing required field",
        },
    },
)

// Create a new error interceptor with a custom callback function
interceptor := reportix.NewErrInterceptor(reportix.ErrInterceptorOpts{
    Callback: func(ctx context.Context, grpcErr error) error {
        // Custom error processing
        return grpcErr
    },
})

// Use the UnaryServerInterceptor for gRPC method error processing
server := grpc.NewServer(
    grpc.UnaryInterceptor(interceptor.UnaryServerInterceptor()),
)

// ... Continue with server setup and registration of services

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorToJSON

func ErrorToJSON(ctx context.Context, err error) error

func NewError

func NewError(
	code codes.Code,
	message string,
	errDetails *errdetails.ErrorInfo,
	debug ...*errdetails.DebugInfo,
) error

Types

type CallbackFunc

type CallbackFunc = func(context.Context, error) error

type ErrInterceptor

type ErrInterceptor struct {
	// contains filtered or unexported fields
}

func NewErrInterceptor

func NewErrInterceptor(opts ErrInterceptorOpts) *ErrInterceptor

func (*ErrInterceptor) UnaryServerInterceptor

func (e *ErrInterceptor) UnaryServerInterceptor() grpc.UnaryServerInterceptor

type ErrInterceptorOpts

type ErrInterceptorOpts struct {
	Callback CallbackFunc
}

type ErrorPayload

type ErrorPayload struct {
	Reason   string            `json:"reason"`
	Domain   string            `json:"domain"`
	Code     uint32            `json:"code"`
	Message  string            `json:"message"`
	Metadata map[string]string `json:"metadata,omitempty"`
}

Jump to

Keyboard shortcuts

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