Skip to content

madbot_cli.errors

Errors raised by the madbot client.

Every error of the package derives from MadbotError. An error of the API is an ApiError, refined by the HTTP status of the answer, so that a caller can catch NotFound without reading status codes.

The message of an ApiError is a sentence for a person: what madbot refused and what to do next, without the URL or the JSON of the answer. raw() gives the request and the answer, for --verbose; as_dict() gives them for --json.

Attributes

NameDescription
BODY_EXCERPTLongest part of an answer kept in the message of an error.
CREDENTIAL_PARAMETERSThe parameters of a connection that carry its credentials, given by the environment.

Classes

NameDescription
ApiErrorA request to the madbot API failed.
ApiUnreachableThe API gave no answer: wrong address, network down, or a timeout.
ConflictThe object is not in a state that allows the request (HTTP 409).
InvalidRequestThe API refused the request (HTTP 400): see code and details.
MadbotErrorBase class of the errors of the package.
NotFoundThe object does not exist, or the account cannot see it (HTTP 404).
NotJsonThe API answered something else than JSON, often the web application instead of the API.
PermissionDeniedThe account may not do this (HTTP 403).
RedirectedThe API sent the request to another address (HTTP 3xx), often https://.
ServerErrorThe API failed (HTTP 500 or more).
TaskFailedA background task of madbot ended in failure.
UnauthorizedThe token is missing, unknown or expired (HTTP 401).
WaitTimeoutSomething madbot does in the background took longer than the client waits.

BODY_EXCERPTattributemodule attribute#

BODY_EXCERPT = 2000

Longest part of an answer kept in the message of an error.

CREDENTIAL_PARAMETERSattributemodule attribute#

CREDENTIAL_PARAMETERS = {'credentials', 'private_params'}

The parameters of a connection that carry its credentials, given by the environment.

ApiErrorclass#

class ApiError(method: str, url: str, status: int | None, body: Any, message: str = '')

Bases: MadbotError

A request to the madbot API failed.

Attributes

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusint or NoneThe HTTP status of the answer, None when the API gave no answer.
bodyAnyThe answer, decoded from JSON when it is JSON, else its text.
codestr or NoneThe code of a madbot error answer, such as body_validation_error.
detailsAnyThe details of a madbot error answer: which parameter is wrong, and why.

Parameters

NameTypeDefaultDescription
methodstrrequiredThe HTTP method of the request.
urlstrrequiredThe URL of the request.
statusint or NonerequiredThe HTTP status of the answer, None when there was no answer.
bodyAnyrequiredThe answer, or what went wrong.
messagestr''A sentence for a person, put before the answer.

bodyattributeinstance attribute#

body = body

codeattributeinstance attribute#

code = body.get('code') if isinstance(body, dict) else None

detailsattributeinstance attribute#

details = body.get('details') if isinstance(body, dict) else None

messageattributeinstance attribute#

message = message or self.explain()

methodattributeinstance attribute#

method = method

statusattributeinstance attribute#

status = status

urlattributeinstance attribute#

url = url

as_dictmethod#

def as_dict() -> dict

Give the request and the answer, as --json shows them.

Returns

dict
method, url, status and the decoded body.

explainmethod#

def explain() -> str

Say in a sentence what madbot answered, for an error built without a message.

Returns

str
What madbot refused, from the details of its answer, without the URL or the JSON.

from_answermethodclassmethod#

def from_answer(method: str, url: str, status: int, body: Any) -> ApiError

Build the error that matches an HTTP status.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer, 400 or more.
bodyAnyThe decoded answer.

Returns

ApiError
A InvalidRequest, Unauthorized, PermissionDenied, NotFound, Conflict or ServerError, or a plain ApiError for another status.

rawmethod#

def raw() -> str

Give the request and the answer, as --verbose shows them.

Returns

str
METHOD URL -> HTTP status, then the answer (cut after BODY_EXCERPT characters).

reasonmethod#

def reason() -> str

Say why madbot refused, from its answer.

Returns

str
Each detail of the answer as parameter: message, joined; the message of the answer when it has no detail; empty when the answer is not a madbot error.

ApiUnreachableclass#

class ApiUnreachable(ApiError)

Bases: ApiError

The API gave no answer: wrong address, network down, or a timeout.

attempts is how many times the request was sent: the client sends again a request that can be repeated without effect.

attemptsattributeclass attributeinstance attribute#

attempts: int = 1

aftermethod#

def after(attempts: int) -> ApiUnreachable

Say the request was sent several times, in the message too.

Parameters

NameTypeDescription
attemptsintHow many times the request was sent.

Returns

ApiUnreachable
The same error.

explainmethod#

def explain() -> str

Say that madbot could not be reached, and where.

Returns

str
The host and the reason.
Inherited from madbot_cli.errors.ApiError

bodyattributeinstance attribute#

body = body

codeattributeinstance attribute#

code = body.get('code') if isinstance(body, dict) else None

detailsattributeinstance attribute#

details = body.get('details') if isinstance(body, dict) else None

messageattributeinstance attribute#

message = message or self.explain()

methodattributeinstance attribute#

method = method

statusattributeinstance attribute#

status = status

urlattributeinstance attribute#

url = url

as_dictmethod#

def as_dict() -> dict

Give the request and the answer, as --json shows them.

Returns

dict
method, url, status and the decoded body.

from_answermethodclassmethod#

def from_answer(method: str, url: str, status: int, body: Any) -> ApiError

Build the error that matches an HTTP status.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer, 400 or more.
bodyAnyThe decoded answer.

Returns

ApiError
A InvalidRequest, Unauthorized, PermissionDenied, NotFound, Conflict or ServerError, or a plain ApiError for another status.

rawmethod#

def raw() -> str

Give the request and the answer, as --verbose shows them.

Returns

str
METHOD URL -> HTTP status, then the answer (cut after BODY_EXCERPT characters).

reasonmethod#

def reason() -> str

Say why madbot refused, from its answer.

Returns

str
Each detail of the answer as parameter: message, joined; the message of the answer when it has no detail; empty when the answer is not a madbot error.

Conflictclass#

class Conflict(ApiError)

Bases: ApiError

The object is not in a state that allows the request (HTTP 409).

explainmethod#

def explain() -> str

Say madbot cannot do it in the present state, and what to run.

Returns

str
Why, when madbot says it, and to run status.
Inherited from madbot_cli.errors.ApiError

bodyattributeinstance attribute#

body = body

codeattributeinstance attribute#

code = body.get('code') if isinstance(body, dict) else None

detailsattributeinstance attribute#

details = body.get('details') if isinstance(body, dict) else None

messageattributeinstance attribute#

message = message or self.explain()

methodattributeinstance attribute#

method = method

statusattributeinstance attribute#

status = status

urlattributeinstance attribute#

url = url

as_dictmethod#

def as_dict() -> dict

Give the request and the answer, as --json shows them.

Returns

dict
method, url, status and the decoded body.

from_answermethodclassmethod#

def from_answer(method: str, url: str, status: int, body: Any) -> ApiError

Build the error that matches an HTTP status.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer, 400 or more.
bodyAnyThe decoded answer.

Returns

ApiError
A InvalidRequest, Unauthorized, PermissionDenied, NotFound, Conflict or ServerError, or a plain ApiError for another status.

rawmethod#

def raw() -> str

Give the request and the answer, as --verbose shows them.

Returns

str
METHOD URL -> HTTP status, then the answer (cut after BODY_EXCERPT characters).

reasonmethod#

def reason() -> str

Say why madbot refused, from its answer.

Returns

str
Each detail of the answer as parameter: message, joined; the message of the answer when it has no detail; empty when the answer is not a madbot error.

InvalidRequestclass#

class InvalidRequest(ApiError)

Bases: ApiError

The API refused the request (HTTP 400): see code and details.

explainmethod#

def explain() -> str

Say which parameter madbot refused, and why.

Returns

str
The parameters refused, and to fix them in the file.
Inherited from madbot_cli.errors.ApiError

bodyattributeinstance attribute#

body = body

codeattributeinstance attribute#

code = body.get('code') if isinstance(body, dict) else None

detailsattributeinstance attribute#

details = body.get('details') if isinstance(body, dict) else None

messageattributeinstance attribute#

message = message or self.explain()

methodattributeinstance attribute#

method = method

statusattributeinstance attribute#

status = status

urlattributeinstance attribute#

url = url

as_dictmethod#

def as_dict() -> dict

Give the request and the answer, as --json shows them.

Returns

dict
method, url, status and the decoded body.

from_answermethodclassmethod#

def from_answer(method: str, url: str, status: int, body: Any) -> ApiError

Build the error that matches an HTTP status.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer, 400 or more.
bodyAnyThe decoded answer.

Returns

ApiError
A InvalidRequest, Unauthorized, PermissionDenied, NotFound, Conflict or ServerError, or a plain ApiError for another status.

rawmethod#

def raw() -> str

Give the request and the answer, as --verbose shows them.

Returns

str
METHOD URL -> HTTP status, then the answer (cut after BODY_EXCERPT characters).

reasonmethod#

def reason() -> str

Say why madbot refused, from its answer.

Returns

str
Each detail of the answer as parameter: message, joined; the message of the answer when it has no detail; empty when the answer is not a madbot error.

MadbotErrorclass#

class MadbotError(Exception)

Bases: Exception

Base class of the errors of the package.

NotFoundclass#

class NotFound(ApiError)

Bases: ApiError

The object does not exist, or the account cannot see it (HTTP 404).

explainmethod#

def explain() -> str

Say madbot does not find the object, and what to run.

Returns

str
Why it may be missing, and to run plan.
Inherited from madbot_cli.errors.ApiError

bodyattributeinstance attribute#

body = body

codeattributeinstance attribute#

code = body.get('code') if isinstance(body, dict) else None

detailsattributeinstance attribute#

details = body.get('details') if isinstance(body, dict) else None

messageattributeinstance attribute#

message = message or self.explain()

methodattributeinstance attribute#

method = method

statusattributeinstance attribute#

status = status

urlattributeinstance attribute#

url = url

as_dictmethod#

def as_dict() -> dict

Give the request and the answer, as --json shows them.

Returns

dict
method, url, status and the decoded body.

from_answermethodclassmethod#

def from_answer(method: str, url: str, status: int, body: Any) -> ApiError

Build the error that matches an HTTP status.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer, 400 or more.
bodyAnyThe decoded answer.

Returns

ApiError
A InvalidRequest, Unauthorized, PermissionDenied, NotFound, Conflict or ServerError, or a plain ApiError for another status.

rawmethod#

def raw() -> str

Give the request and the answer, as --verbose shows them.

Returns

str
METHOD URL -> HTTP status, then the answer (cut after BODY_EXCERPT characters).

reasonmethod#

def reason() -> str

Say why madbot refused, from its answer.

Returns

str
Each detail of the answer as parameter: message, joined; the message of the answer when it has no detail; empty when the answer is not a madbot error.

NotJsonclass#

class NotJson(ApiError)

Bases: ApiError

The API answered something else than JSON, often the web application instead of the API.

Inherited from madbot_cli.errors.ApiError

bodyattributeinstance attribute#

body = body

codeattributeinstance attribute#

code = body.get('code') if isinstance(body, dict) else None

detailsattributeinstance attribute#

details = body.get('details') if isinstance(body, dict) else None

messageattributeinstance attribute#

message = message or self.explain()

methodattributeinstance attribute#

method = method

statusattributeinstance attribute#

status = status

urlattributeinstance attribute#

url = url

as_dictmethod#

def as_dict() -> dict

Give the request and the answer, as --json shows them.

Returns

dict
method, url, status and the decoded body.

explainmethod#

def explain() -> str

Say in a sentence what madbot answered, for an error built without a message.

Returns

str
What madbot refused, from the details of its answer, without the URL or the JSON.

from_answermethodclassmethod#

def from_answer(method: str, url: str, status: int, body: Any) -> ApiError

Build the error that matches an HTTP status.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer, 400 or more.
bodyAnyThe decoded answer.

Returns

ApiError
A InvalidRequest, Unauthorized, PermissionDenied, NotFound, Conflict or ServerError, or a plain ApiError for another status.

rawmethod#

def raw() -> str

Give the request and the answer, as --verbose shows them.

Returns

str
METHOD URL -> HTTP status, then the answer (cut after BODY_EXCERPT characters).

reasonmethod#

def reason() -> str

Say why madbot refused, from its answer.

Returns

str
Each detail of the answer as parameter: message, joined; the message of the answer when it has no detail; empty when the answer is not a madbot error.

PermissionDeniedclass#

class PermissionDenied(ApiError)

Bases: ApiError

The account may not do this (HTTP 403).

explainmethod#

def explain() -> str

Say the account may not do it, and whom to ask.

Returns

str
Why, when madbot says it, and whom to ask.
Inherited from madbot_cli.errors.ApiError

bodyattributeinstance attribute#

body = body

codeattributeinstance attribute#

code = body.get('code') if isinstance(body, dict) else None

detailsattributeinstance attribute#

details = body.get('details') if isinstance(body, dict) else None

messageattributeinstance attribute#

message = message or self.explain()

methodattributeinstance attribute#

method = method

statusattributeinstance attribute#

status = status

urlattributeinstance attribute#

url = url

as_dictmethod#

def as_dict() -> dict

Give the request and the answer, as --json shows them.

Returns

dict
method, url, status and the decoded body.

from_answermethodclassmethod#

def from_answer(method: str, url: str, status: int, body: Any) -> ApiError

Build the error that matches an HTTP status.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer, 400 or more.
bodyAnyThe decoded answer.

Returns

ApiError
A InvalidRequest, Unauthorized, PermissionDenied, NotFound, Conflict or ServerError, or a plain ApiError for another status.

rawmethod#

def raw() -> str

Give the request and the answer, as --verbose shows them.

Returns

str
METHOD URL -> HTTP status, then the answer (cut after BODY_EXCERPT characters).

reasonmethod#

def reason() -> str

Say why madbot refused, from its answer.

Returns

str
Each detail of the answer as parameter: message, joined; the message of the answer when it has no detail; empty when the answer is not a madbot error.

Redirectedclass#

class Redirected(ApiError)

Bases: ApiError

The API sent the request to another address (HTTP 3xx), often https://.

The client does not follow: the token would go to the new address, and a POST would come back as a GET. location is the address given, if any.

locationattributeclass attributeinstance attribute#

location: str | None = None

ofmethodclassmethod#

def of(method: str, url: str, status: int, location: str | None) -> Redirected

Build the error, with the address to use instead.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer.
locationstr or NoneThe address the API sends to, None when unknown.

Returns

Redirected
The error.
Inherited from madbot_cli.errors.ApiError

bodyattributeinstance attribute#

body = body

codeattributeinstance attribute#

code = body.get('code') if isinstance(body, dict) else None

detailsattributeinstance attribute#

details = body.get('details') if isinstance(body, dict) else None

messageattributeinstance attribute#

message = message or self.explain()

methodattributeinstance attribute#

method = method

statusattributeinstance attribute#

status = status

urlattributeinstance attribute#

url = url

as_dictmethod#

def as_dict() -> dict

Give the request and the answer, as --json shows them.

Returns

dict
method, url, status and the decoded body.

explainmethod#

def explain() -> str

Say in a sentence what madbot answered, for an error built without a message.

Returns

str
What madbot refused, from the details of its answer, without the URL or the JSON.

from_answermethodclassmethod#

def from_answer(method: str, url: str, status: int, body: Any) -> ApiError

Build the error that matches an HTTP status.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer, 400 or more.
bodyAnyThe decoded answer.

Returns

ApiError
A InvalidRequest, Unauthorized, PermissionDenied, NotFound, Conflict or ServerError, or a plain ApiError for another status.

rawmethod#

def raw() -> str

Give the request and the answer, as --verbose shows them.

Returns

str
METHOD URL -> HTTP status, then the answer (cut after BODY_EXCERPT characters).

reasonmethod#

def reason() -> str

Say why madbot refused, from its answer.

Returns

str
Each detail of the answer as parameter: message, joined; the message of the answer when it has no detail; empty when the answer is not a madbot error.

ServerErrorclass#

class ServerError(ApiError)

Bases: ApiError

The API failed (HTTP 500 or more).

explainmethod#

def explain() -> str

Say madbot failed, and what to do.

Returns

str
The status, what madbot says, and to try again then tell the administrators.
Inherited from madbot_cli.errors.ApiError

bodyattributeinstance attribute#

body = body

codeattributeinstance attribute#

code = body.get('code') if isinstance(body, dict) else None

detailsattributeinstance attribute#

details = body.get('details') if isinstance(body, dict) else None

messageattributeinstance attribute#

message = message or self.explain()

methodattributeinstance attribute#

method = method

statusattributeinstance attribute#

status = status

urlattributeinstance attribute#

url = url

as_dictmethod#

def as_dict() -> dict

Give the request and the answer, as --json shows them.

Returns

dict
method, url, status and the decoded body.

from_answermethodclassmethod#

def from_answer(method: str, url: str, status: int, body: Any) -> ApiError

Build the error that matches an HTTP status.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer, 400 or more.
bodyAnyThe decoded answer.

Returns

ApiError
A InvalidRequest, Unauthorized, PermissionDenied, NotFound, Conflict or ServerError, or a plain ApiError for another status.

rawmethod#

def raw() -> str

Give the request and the answer, as --verbose shows them.

Returns

str
METHOD URL -> HTTP status, then the answer (cut after BODY_EXCERPT characters).

reasonmethod#

def reason() -> str

Say why madbot refused, from its answer.

Returns

str
Each detail of the answer as parameter: message, joined; the message of the answer when it has no detail; empty when the answer is not a madbot error.

TaskFailedclass#

class TaskFailed(task: dict)

Bases: MadbotError

A background task of madbot ended in failure.

Attributes

NameTypeDescription
taskdictThe task as the API returns it, with its status and result.

Parameters

NameTypeDescription
taskdictThe task as the API returns it.

taskattributeinstance attribute#

task = task

Unauthorizedclass#

class Unauthorized(ApiError)

Bases: ApiError

The token is missing, unknown or expired (HTTP 401).

explainmethod#

def explain() -> str

Say the token is refused, and what token to give.

Returns

str
What to check.
Inherited from madbot_cli.errors.ApiError

bodyattributeinstance attribute#

body = body

codeattributeinstance attribute#

code = body.get('code') if isinstance(body, dict) else None

detailsattributeinstance attribute#

details = body.get('details') if isinstance(body, dict) else None

messageattributeinstance attribute#

message = message or self.explain()

methodattributeinstance attribute#

method = method

statusattributeinstance attribute#

status = status

urlattributeinstance attribute#

url = url

as_dictmethod#

def as_dict() -> dict

Give the request and the answer, as --json shows them.

Returns

dict
method, url, status and the decoded body.

from_answermethodclassmethod#

def from_answer(method: str, url: str, status: int, body: Any) -> ApiError

Build the error that matches an HTTP status.

Parameters

NameTypeDescription
methodstrThe HTTP method of the request.
urlstrThe URL of the request.
statusintThe HTTP status of the answer, 400 or more.
bodyAnyThe decoded answer.

Returns

ApiError
A InvalidRequest, Unauthorized, PermissionDenied, NotFound, Conflict or ServerError, or a plain ApiError for another status.

rawmethod#

def raw() -> str

Give the request and the answer, as --verbose shows them.

Returns

str
METHOD URL -> HTTP status, then the answer (cut after BODY_EXCERPT characters).

reasonmethod#

def reason() -> str

Say why madbot refused, from its answer.

Returns

str
Each detail of the answer as parameter: message, joined; the message of the answer when it has no detail; empty when the answer is not a madbot error.

WaitTimeoutclass#

class WaitTimeout(MadbotError)

Bases: MadbotError

Something madbot does in the background took longer than the client waits.