Skip to content

Commit

Permalink
Extra data for errors (#101)
Browse files Browse the repository at this point in the history
Co-authored-by: Piotr Dyba <piotr@dyba.it>
Co-authored-by: Grzegorz Redlicki <redlicki.grzegorz@gmail.com>
  • Loading branch information
3 people authored May 21, 2024
1 parent d2ba12a commit fa1d26e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ Released 2024-01-31
- Extends Response class to be more useful by adding `json` method
- Migrates away from `datetime.utwnow()` to `datetime.now(UTC)`

### Version 0.6.6
Released 2024-05-24

- Extends `LambdaFWException` with extra property for easier handling edge case exceptions
- Dumps `extra` data into the response when raising an error during handling requests


### Version 0.7.0
Release ETA 2024-02-31 ;)
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ bandit:
.PHONY: pip-audit
pip-audit:
pip-audit --version
# TODO: Fix the issue with the vulnerable ecdsa library
pip-audit -r requirements.txt --ignore-vuln GHSA-wj6h-64fc-37mp
# TODO: Fix the issue with the vulnerable ecdsa and jose libraries
pip-audit -r requirements.txt --ignore-vuln GHSA-wj6h-64fc-37mp --ignore-vuln GHSA-cjwg-qfpm-7377 --ignore-vuln GHSA-6c5p-j8vq-pqhj

.PHONY: secure
secure: bandit pip-audit
Expand Down
12 changes: 7 additions & 5 deletions lbz/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ class LambdaFWException(Exception):
status_code = HTTPStatus.INTERNAL_SERVER_ERROR.value
error_code: str | None = None

def __init__(self, message: str = "", error_code: str | None = None) -> None:
def __init__(
self, message: str = "", error_code: str | None = None, extra: dict | None = None
) -> None:
super().__init__(message)
if message:
self.message = message
if error_code:
self.error_code = error_code

self.message = message or self.message
self.error_code = error_code or self.error_code
self.extra = extra or {}

def __str__(self) -> str:
if self.error_code is not None:
Expand Down
3 changes: 2 additions & 1 deletion lbz/response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
from copy import deepcopy

from lbz.exceptions import LambdaFWException
from lbz.misc import deprecated
Expand Down Expand Up @@ -34,7 +35,7 @@ def __repr__(self) -> str:
@classmethod
def from_exception(cls, error: LambdaFWException, request_id: str) -> Response:
"""Creates a proper standardised Response for Errors."""
resp_data = {"message": error.message, "request_id": request_id}
resp_data = {**deepcopy(error.extra), "message": error.message, "request_id": request_id}
if error.error_code:
resp_data["error_code"] = error.error_code

Expand Down
6 changes: 5 additions & 1 deletion tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ class TestException(LambdaFWException):
assert exception.message == "No error message"
assert exception.error_code == "TEST_ERR"
assert exception.status_code == 444
assert exception.extra == {}


def test__lambda_fw_exception__respects_values_provided_during_initialization() -> None:
exception = LambdaFWException(message="Test error message", error_code="TEST_ERR")
exception = LambdaFWException(
message="Test error message", error_code="TEST_ERR", extra={"fun": "stuff"}
)

assert str(exception) == "[500] TEST_ERR - Test error message"
assert exception.message == "Test error message"
assert exception.error_code == "TEST_ERR"
assert exception.status_code == 500
assert exception.extra == {"fun": "stuff"}


def test__unsupported_method__builds_message_based_on_method_provided_from_outside() -> None:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ class RandomException(LambdaFWException):
"statusCode": 500,
}

def test__from_exception__adds_extra_data_to_response_body_if_only_declared(self) -> None:
class RandomException(LambdaFWException):
error_code = "RAND001"

response = Response.from_exception(
RandomException(extra={"added_data": "random"}), "req-id"
)

assert response.headers == {"Content-Type": ContentType.JSON}
assert response.is_base64 is False
assert response.status_code == 500
assert response.body == {
"added_data": "random",
"message": "Server got itself in trouble",
"request_id": "req-id",
"error_code": "RAND001",
}

@pytest.mark.parametrize(
"body",
[
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.5
0.6.6

0 comments on commit fa1d26e

Please sign in to comment.