interpreter

package
v0.0.0-...-4cf73da Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

nolint:ireturn

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyResponse = errors.New("empty provider response")
View Source
var ErrUnknownResponseFormat = errors.New("unknown response format")

Functions

func DefaultStatusCodeMappingToErr

func DefaultStatusCodeMappingToErr(res *http.Response, body []byte) error

Types

type DirectFaultyResponder

type DirectFaultyResponder struct {
	Callback func(res *http.Response, body []byte) error
}

DirectFaultyResponder is an implementation of FaultyResponseHandler. It is a simple wrapper that allows you to directly provide an implementation of handler callback. First consider FaultyResponder that reduces the boilerplate.

func (DirectFaultyResponder) HandleErrorResponse

func (r DirectFaultyResponder) HandleErrorResponse(res *http.Response, body []byte) error

type ErrorDescriptor

type ErrorDescriptor interface {
	CombineErr(base error) error
}

ErrorDescriptor enhances base error with extra message. Every implementor decides how server response will be converted, and how important message will be formated into helpful error.

type ErrorHandler

type ErrorHandler struct {
	JSON   FaultyResponseHandler
	XML    FaultyResponseHandler
	HTML   FaultyResponseHandler
	Custom map[Mime]FaultyResponseHandler
}

ErrorHandler invokes a function that matches response media type with parse error, ex: JSON<->JsonParserMethod otherwise defaults to general opaque error interpretation.

func (ErrorHandler) Handle

func (h ErrorHandler) Handle(res *http.Response, body []byte) error

type FaultyResponder

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

FaultyResponder is an implementation of FaultyResponseHandler. It uses common techniques to handle error response returned by provider.

func NewFaultyResponder

func NewFaultyResponder(errorSwitch *FormatSwitch, statusCodeMap map[int]error) *FaultyResponder

NewFaultyResponder creates error responder that will be used when provider responds with erroneous payload.

  • FormatSwitch will be used to select the best matching error format. It can be null if you don't want pretty parsing and formating.
  • StatusCodeMap will be used to enhance error message. This is an optional map that will precede any default status to error mapping.

func (FaultyResponder) HandleErrorResponse

func (r FaultyResponder) HandleErrorResponse(res *http.Response, body []byte) error

type FaultyResponseHandler

type FaultyResponseHandler interface {
	HandleErrorResponse(res *http.Response, body []byte) error
}

FaultyResponseHandler used to parse erroneous response.

type FormatSwitch

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

FormatSwitch allows to select the most appropriate format. Switch will traverse every template stopping at the closest match, which best describes server response. Then ErrorDescriptor will convert itself into a composite go error.

func NewFormatSwitch

func NewFormatSwitch(templates ...FormatTemplate) *FormatSwitch

func (FormatSwitch) ParseJSON

func (s FormatSwitch) ParseJSON(data []byte) ErrorDescriptor

ParseJSON selects a template, populates it, and returns the result. If the error response is concise and clear, it’s used directly; otherwise, the entire response is used to build an error. This strategy ensures complete visibility into the provider’s response.

The JSON data may either contain a single object, interpreted as an ErrorDescriptor, or an array of objects, with each mapped to matching ErrorDescriptor.

type FormatTemplate

type FormatTemplate struct {
	// MustKeys is a list of important keys that if all present will signify the match for Template.
	MustKeys []string
	// Template is a factory that returns a struct, which will be used to flush the data into.
	Template func() ErrorDescriptor
}

FormatTemplate holds concrete struct that represent erroneous server response. It is used by FormatSwitch.

type Mime

type Mime = string

Mime is the name of media type. Example: "application/json".

Jump to

Keyboard shortcuts

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