1"""
2This module contains a Python interface for Problem Details for HTTP APIs
3<https://tools.ietf.org/html/draft-ietf-appsawg-http-problem-00>, which is a standardized format
4to communicate distinct "problem types" to non-human consumers.
5"""
6
7import json
8
9
10def problem(status, title, detail, type=None, instance=None, headers=None, ext=None):
11 """
12 Returns a `Problem Details <https://tools.ietf.org/html/draft-ietf-appsawg-http-problem-00>`_ error response.
13
14
15 :param status: The HTTP status code generated by the origin server for this occurrence of the problem.
16 :type status: int
17 :param title: A short, human-readable summary of the problem type. It SHOULD NOT change from occurrence to
18 occurrence of the problem, except for purposes of localisation.
19 :type title: str
20 :param detail: An human readable explanation specific to this occurrence of the problem.
21 :type detail: str
22 :param type: An absolute URI that identifies the problem type. When dereferenced, it SHOULD provide human-readable
23 documentation for the problem type (e.g., using HTML). When this member is not present its value is
24 assumed to be "about:blank".
25 :type: type: str
26 :param instance: An absolute URI that identifies the specific occurrence of the problem. It may or may not yield
27 further information if dereferenced.
28 :type instance: str
29 :param headers: HTTP headers to include in the response
30 :type headers: dict | None
31 :param ext: Extension members to include in the body
32 :type ext: dict | None
33 :return: error response
34 :rtype: ConnexionResponse
35 """
36 from .lifecycle import ConnexionResponse # prevent circular import
37
38 if not type:
39 type = "about:blank"
40
41 problem_response = {
42 "type": type,
43 "title": title,
44 "detail": detail,
45 "status": status,
46 }
47 if instance:
48 problem_response["instance"] = instance
49 if ext:
50 problem_response.update(ext)
51
52 mimetype = content_type = "application/problem+json"
53
54 return ConnexionResponse(
55 status,
56 mimetype,
57 content_type,
58 body=json.dumps(problem_response),
59 headers=headers,
60 )