MySQL failures as SqlError reasons (the classifyError of Effect's
@effect/sql-mysql2 MysqlClient).
A server error carries an errno (asyncmy's args[0]); classify maps
the errnos Effect knows to reasons and every other failure (the client's
own, such as a refused connection) to UnknownError. A duplicate entry
(1062) names its key from a constraint attribute, else from the
message's for key 'name'.
Functions#
duplicate_entry_constraint#
duplicate_entry_constraint(cause) -> strThe key a duplicate entry broke: the error's constraint
attribute when it has one, else what its message names.
| Parameter | Type | Description |
|---|---|---|
cause | BaseException |
Returns str
def duplicate_entry_constraint(cause: BaseException) -> str:
"""The key a duplicate entry broke: the error's ``constraint``
attribute when it has one, else what its message names."""
explicit = _normalize(getattr(cause, "constraint", None))
if explicit != _UNKNOWN_CONSTRAINT:
return explicit
message = cause.args[1] if len(cause.args) > 1 else None
return _constraint_from_message(message)classify#
classify(cause, *, message, operation) -> SqlErrorReasonThe reason for an asyncmy error (or any other cause).
| Parameter | Type | Description |
|---|---|---|
cause | object | |
message | str | |
operation | str |
Returns SqlErrorReason
def classify(cause: object, *, message: str, operation: str) -> SqlErrorReason:
"""The reason for an asyncmy error (or any other cause)."""
errno = _errno(cause)
if errno == _DUPLICATE_ENTRY and isinstance(cause, BaseException):
return UniqueViolation(
cause=cause,
message=message,
operation=operation,
constraint=duplicate_entry_constraint(cause),
)
reason = UnknownError if errno is None else _BY_ERRNO.get(errno, UnknownError)
return reason(cause=cause, message=message, operation=operation)catch#
catch(message, operation) -> Callable[[BaseException], SqlError]Turns an exception into the SqlError of operation.
| Parameter | Type | Description |
|---|---|---|
message | str | |
operation | str |
Returns Callable[[BaseException], SqlError]
def catch(message: str, operation: str) -> Callable[[BaseException], SqlError]:
"""Turns an exception into the ``SqlError`` of ``operation``."""
return lambda cause: SqlError(reason=classify(cause, message=message, operation=operation))