linode_api4-python icon indicating copy to clipboard operation
linode_api4-python copied to clipboard

Improve ApiError message formatting; add `response` field to ApiError and UnexpectedResponseError

Open lgarber-akamai opened this issue 4 months ago • 0 comments

📝 Description

This pull request improves the ApiError message formatting logic to include the method and path of the failed request. This is done through a new ApiError.from_request(...) class method, which as a bonus simplifies and unifies API error construction across the project.

✔️ How to Test

The following test steps assume you have pulled down this PR locally and run make install.

Unit Testing

make testunit

Integration Testing

make testint

Manual Testing

  1. In a linode_api4-python sandbox environment (e.g. dx-devenv), run the following:
import os

from linode_api4 import LinodeClient, ApiError

# Ensure non-API errors are handled gracefully
try:
    LinodeClient(token="foobar", base_url="https://example.com").get("/fake/endpoint")
except ApiError as exc:
    print(exc)

# Ensure bad token errors are handled gracefully
try:
    LinodeClient(token="foobar").linode.instances()
except ApiError as exc:
    print(exc)

client = LinodeClient(token=os.getenv("LINODE_TOKEN"))

# Ensure invalid endpoint errors are handled gracefully
try:
    client.get("/fake/endpoint")
except ApiError as exc:
    print(exc)

# Ensure invalid request errors are handled gracefully
try:
    client.linode.instance_create("fake-type", "fake-region")
except ApiError as exc:
    print(exc)
  1. Ensure the output matches the following:
GET /fake/endpoint: [404] <!doctype html>
<html>
...
</html>

GET /v4/linode/instances: [401] Invalid Token
GET /v4/fake/endpoint: [404] Not found
POST /v4/linode/instances: [400] type: A valid plan type by that ID was not found; region: region is not valid

lgarber-akamai avatar Oct 14 '24 18:10 lgarber-akamai