problem

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 7 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const HTTPContentType = "application/problem+json"

HTTPContentType is the value used in HTTP requests and responses for the Content-Type header. This is defined by the IETF RFC-7807 and can therefore not be changed.

Variables

View Source
var DocsHost = "wharf.iver.com"

DocsHost is the host name of the documentation page. Use in various helper functions as a fallback if no host is provided.

Functions

func ConvertURLToAbsDocsURL

func ConvertURLToAbsDocsURL(u url.URL) *url.URL

ConvertURLToAbsDocsURL adds schema and sets the host if that has not been set.

Example
var u *url.URL

u, _ = url.Parse("https://wharf.iver.com/#/prob/build/run/invalid-input")
fmt.Println("Unaltered 1:", ConvertURLToAbsDocsURL(*u).String() == u.String())

u, _ = url.Parse("http://some-other-page/prob/build/run/invalid-input")
fmt.Println("Unaltered 2:", ConvertURLToAbsDocsURL(*u).String() == u.String())

u, _ = url.Parse("https://wharf.iver.com/prob/build/run/invalid-input")
fmt.Println("Fragmented path:", ConvertURLToAbsDocsURL(*u).String())

u, _ = url.Parse("/prob/build/run/invalid-input")
fmt.Println("Added schema & host:", ConvertURLToAbsDocsURL(*u).String())

u, _ = url.Parse("/prob/build/run/invalid-input")
fmt.Println("Leaves original intact:", ConvertURLToAbsDocsURL(*u).String() != u.String())
Output:

Unaltered 1: true
Unaltered 2: true
Fragmented path: https://wharf.iver.com/#/prob/build/run/invalid-input
Added schema & host: https://wharf.iver.com/#/prob/build/run/invalid-input
Leaves original intact: true

func IsHTTPResponse

func IsHTTPResponse(response *http.Response) bool

IsHTTPResponse returns true if the HTTP response has the Content-Type of a problem response:

Content-Type: application/problem+json

Types

type Response

type Response struct {
	// Type is a URI reference that identifies the problem type. The IETF
	// RFC-7807 specification encourages that, when dereferenced, it provide
	// human-readable documentation for the problem type (e.g., using HTML).
	// When this member is not present, its value is assumed to be
	// "about:blank".
	Type string `json:"type" example:"https://wharf.iver.com/#/prob/build/run/invalid-input"`

	// Title is a short, human-readable summary of the problem type.
	// It SHOULD NOT change from occurrence to ocurrence of the problem, except
	// for purposes of localization.
	//
	// Recommended to be kept brief, have proper punctuation, and be
	// capitalized, like a short sentence.
	Title string `json:"title" example:"Invalid input variable for build."`

	// Status is the HTTP status code generated by the origin server for this
	// occurrence of the problem.
	Status int `json:"status" example:"400"`

	// Detail is a human-readable explanation specific to this occurrence of the
	// problem.
	//
	// Recommended to have proper punctuation, and be capitalized,
	// like a sentence. Compared to Title this field may stretch on and be
	// longer.
	Detail string `json:"detail" example:"Build requires input variable 'myInput' to be of type 'string', but got 'int' instead."`

	// Instance is a URI reference that identifies the specific occurrence of
	// the problem. It may or may not yield further information if dereferenced.
	Instance string `json:"instance" example:"/projects/12345/builds/run/6789"`

	// Error is an extended field for the regular Problem model defined in
	// RFC-7807. It contains the string message of the error (if any).
	Errors []string `json:"errors" example:"strconv.ParseUint: parsing \"-1\": invalid syntax"`
}

Response can be serialized into JSON and its fields follow the problem schema defined by IETF RFC-7807.

This type also conforms to the error interface by using the title and list of errors as the error message.

func ParseHTTPResponse

func ParseHTTPResponse(response *http.Response) (Response, error)

ParseHTTPResponse attempts to unmarshal the body response as a problem response. No validation check is made, so the user of this function is assumed to check if the body is of the correct content type before calling this function, for example via the IsHTTPResponse function.

Example
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"

	"github.com/iver-wharf/wharf-core/v2/pkg/problem"
)

func main() {
	req := httptest.NewRequest("GET", "/projects/123", nil)

	// Faking a HTTP response here
	req.Response = &http.Response{
		Body: io.NopCloser(strings.NewReader(`
{
  "type": "https://wharf.iver.com/#/prob/build/run/invalid-input",
  "title": "Invalid input variable for build.",
  "status": 400,
  "detail": "Build requires input variable 'myInput' to be of type 'string', but got 'int' instead.",
  "instance": "/projects/12345/builds/run/6789",
  "errors": [
    "strconv.ParseUint: parsing \"-1\": invalid syntax"
  ]
}
`)),
		Header: make(http.Header),
	}
	req.Response.Header.Add("Content-Type", problem.HTTPContentType)

	if problem.IsHTTPResponse(req.Response) {
		p, err := problem.ParseHTTPResponse(req.Response)
		if err != nil {
			panic(err)
		}

		fmt.Println(p.String())
	}

}
Output:

{(problem) HTTP 400, https://wharf.iver.com/#/prob/build/run/invalid-input
    Title: Invalid input variable for build.
   Detail: Build requires input variable 'myInput' to be of type 'string', but got 'int' instead.
 Error(s): [strconv.ParseUint: parsing "-1": invalid syntax]
 Instance: /projects/12345/builds/run/6789 }

func (Response) Error

func (r Response) Error() string

func (Response) String

func (r Response) String() string

Jump to

Keyboard shortcuts

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