Skip to content

Handling response codes in a DRY way #11

@iftheshoefritz

Description

@iftheshoefritz

I understand that it's useful to be able to check http response codes once you've received a value from the API. However doing something like this:

response = Pandadoc::Api::Document.download
case response.code
when 429
  # handle rate limiting
when 4xx
  # handle bad client input
when 5xx
  # handle no response

... in my code for every different call seems bad. I'm considering contributing a patch and wondering about direction:

  • expose bang methods Document.download!, Document.create! that raise e.g. Pandadoc::Api::TooManyRequestsError for 429 (I won't just change the original methods... assuming that people will not appreciate it if suddenly this library starts raising errors when they expect to be able to safely inspect response codes)
  • alternatively, adding info to the object returned by the client: response.success? etc. Admittedly I'm not sure what info I'd add for rate limiting if I did this

This was all sparked by finding out that after 9 months of thinking this library was safely giving us PDFs, we found out that most of the "PDF" files we saved were actually just JSON: {"type"=>"request_error", "detail"=>"Request was throttled. Expected available in 1 second."}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions