Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typing in error.py #1356

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions minio/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@

"""

from __future__ import absolute_import, annotations

from typing import Type, TypeVar
from xml.etree import ElementTree as ET

from urllib3.response import BaseHTTPResponse

from .xml import findtext


Expand All @@ -40,7 +45,7 @@ class MinioException(Exception):
class InvalidResponseError(MinioException):
"""Raised to indicate that non-xml response from server."""

def __init__(self, code, content_type, body):
def __init__(self, code: int, content_type: str | None, body: str | None):
self._code = code
self._content_type = content_type
self._body = body
Expand All @@ -56,24 +61,36 @@ def __reduce__(self):
class ServerError(MinioException):
"""Raised to indicate that S3 service returning HTTP server error."""

def __init__(self, message, status_code):
def __init__(self, message: str, status_code: int):
self._status_code = status_code
super().__init__(message)

@property
def status_code(self):
def status_code(self) -> int:
"""Get HTTP status code."""
return self._status_code


A = TypeVar("A", bound="S3Error")


class S3Error(MinioException):
"""
Raised to indicate that error response is received
when executing S3 operation.
"""

def __init__(self, code, message, resource, request_id, host_id,
response, bucket_name=None, object_name=None):
def __init__(
self,
code: str | None,
message: str | None,
resource: str | None,
request_id: str | None,
host_id: str | None,
response: BaseHTTPResponse,
bucket_name: str | None = None,
object_name: str | None = None,
):
self._code = code
self._message = message
self._resource = resource
Expand Down Expand Up @@ -103,22 +120,22 @@ def __reduce__(self):
self._bucket_name, self._object_name)

@property
def code(self):
def code(self) -> str | None:
"""Get S3 error code."""
return self._code

@property
def message(self):
def message(self) -> str | None:
"""Get S3 error message."""
return self._message

@property
def response(self):
def response(self) -> BaseHTTPResponse:
"""Get HTTP response."""
return self._response

@classmethod
def fromxml(cls, response):
def fromxml(cls: Type[A], response: BaseHTTPResponse) -> A:
"""Create new object with values from XML element."""
element = ET.fromstring(response.data.decode())
return cls(
Expand All @@ -132,7 +149,7 @@ def fromxml(cls, response):
response=response,
)

def copy(self, code, message):
def copy(self, code: str, message: str) -> S3Error:
"""Make a copy with replace code and message."""
return S3Error(
code,
Expand All @@ -149,7 +166,7 @@ def copy(self, code, message):
class MinioAdminException(Exception):
"""Raised to indicate admin API execution error."""

def __init__(self, code, body):
def __init__(self, code: str, body: str):
self._code = code
self._body = body
super().__init__(
Expand Down