binarylane / binarylane-cli

Command-line interface to BinaryLane API
MIT License
15 stars 3 forks source link

Format 4xx/5xx status codes and return error code #2

Closed david-pershouse closed 1 year ago

david-pershouse commented 1 year ago

When a request receives a response with 4xx or 5xx status codes, it is now treated as an error and bl will return error level of -1. This MR also applies custom formatting to the ProblemDetails and ValidationProblemDetails objects that are common in the error response.

ProblemDetails (usually with 5xx errors) is just rendered as ERROR: {title} - {detail} where detail is optional

eg for a 404

PS C:\dev\vps\python-blcli> poetry run bl server action resize 33
ERROR:  Server not found.

ValidationProblemDetails (4xx validation errors) is similar to problem details, but includes the errors property as a table

PS C:\dev\python-blcli> bl server create --size blah --region blah2  --image blkah
Error: One or more validation errors occurred.
┌────────┬─────────────────────────────┐
│ field  │ errors                      │
├────────┼─────────────────────────────┤
│ size   │ size not found.             │
│ image  │ Operating system not found. │
│ region │ Facility not found.         │
└────────┴─────────────────────────────┘
david-pershouse commented 1 year ago

This has now been changed to obey the --output argument. This required some refactoring of the printer classes. The main change is the introduction of an error() method to the printers which is like print but outputs to stderr. The error function renders any tables including their headings and with all fields (as it is assumed the these options were specific to success response types).

To avoid passing output file down to each subclass of printer, the printer sub-classes now just return string.

I tried to use the built in "title" property of terminaltables, but could not get it to render correctly (or at all) in windows, so this PR just prints a single line title.

david-pershouse commented 1 year ago

The bulk of this was addressed in #10