-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from tigattack/refactor/err_handling_etc
Improve error handling, style, other tweaks
- Loading branch information
Showing
6 changed files
with
152 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Errors for the DVLA Vehicle Enquiry Service API""" | ||
|
||
from typing import Any, Optional | ||
|
||
|
||
class VehicleEnquiryError(Exception): | ||
"""Custom exception for vehicle enquiry errors, encapsulating error details.""" | ||
|
||
def __init__( | ||
self, | ||
title: str, | ||
status: Optional[int] = None, | ||
code: Optional[int] = None, | ||
detail: Optional[str] = None, | ||
errors: Optional[list[dict[str, Any]]] = None, | ||
): | ||
# Support both error dict and list[dict] of errors | ||
if errors: | ||
if len(errors) == 1: | ||
# Handle single error presented as list | ||
error = errors[0] | ||
_status = error.get("status") | ||
_code = error.get("code") | ||
if isinstance(_status, str): | ||
status = int(_status) | ||
if isinstance(_code, str): | ||
code = int(_code) | ||
|
||
title = error.get("title") or title | ||
detail = error.get("detail") or detail | ||
super().__init__(f"[{error.get('status')}] {error.get('title')}: {error.get('detail') or 'No details'}") | ||
else: | ||
# Convert multiple error details into a string | ||
error_messages = " ; ".join( | ||
f"[{error.get('status')}] {error.get('title')}: {error.get('detail') or 'No details'}" | ||
for error in errors | ||
) | ||
super().__init__(f"Multiple errors occurred: {error_messages}") | ||
else: | ||
# Handle a single error | ||
super().__init__(f"[{status}] {title}: {detail or 'No details'}") | ||
|
||
self.title = title | ||
self.status = status | ||
self.code = code | ||
self.detail = detail | ||
self.errors = errors or [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.