Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Handling #5

Open
mattkuras opened this issue May 1, 2023 · 0 comments
Open

Error Handling #5

mattkuras opened this issue May 1, 2023 · 0 comments

Comments

@mattkuras
Copy link

I'm trying to create a data request object. I have this code

client = DocSpring::Client.new
template_id = "tpl_4tgyRQpC56Rcxzh2sg"
response = client.generate_pdf(
  template_id: template_id,
  test: true,
  data: i_nine_data
)
 response.submission

and it properly creates the response with the pdf

however when I add a key for the data request like below, I get a fatal error DocSpring::ApiError (: [Could not parse errors from response body]):

client = DocSpring::Client.new
template_id = "tpl_4tgyRQpC56Rcxzh2sg"
response = client.generate_pdf(
      template_id: template_id,
      test: true,
      data: i_nine_data,
      data_requests: data_request
)
    response.submission

From digging into the gem's code in lib/docspring/api_client.rb, I suspect there might be a bug in the error handling. The error response should have been {"status"=>"error", "errors"=>{"submission_data_requests"=>["is invalid"]}} ( which is still a fairly unhelpful error). However the error handling checks if the response_json['errors'] is_a? Array - its not its a hash; and it checks for the existence of response_json['error'] - which doesn't because the key in the response is called errors not error

since it doesn't find an error message even thought it exists, it instead sends a default message of [Could not parse errors from response body]. Here's the full method code

  def call_api(http_method, path, opts = {})
      request = build_request(http_method, path, opts)
      response = request.run

      if @config.debugging
        @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
      end

      unless response.success?
        if response.timed_out?
          fail ApiError.new('Connection timed out')
        elsif response.code == 0
          # Errors from libcurl will be made visible here
          fail ApiError.new(:code => 0,
                            :message => response.return_message)
        else
          exception_message = nil
          response_json = JSON.parse(response.body) rescue nil
          if response_json.is_a? Hash
            if response_json['errors'].is_a? Array
              response_errors = response_json['errors'].join(', ')
            elsif response_json['error']
              response_errors = response_json['error']
            end
            if response_errors
              exception_message = "#{response.status_message}: #{response_errors}"
            end
          end

          unless exception_message
            exception_message = "#{response.status_message}: [Could not parse errors from response body]"
          end

          fail ApiError.new(:code => response.code,
                            :response_headers => response.headers,
                            :response_body => response.body),
               exception_message
        end
      end

      if opts[:return_type]
        data = deserialize(response, opts[:return_type])
      else
        data = nil
      end
      return data, response.code, response.headers
    end

Could we get proper errors for the future? I also was getting an error for every submission last week because our free trial had ended, but the API provided exactly zero errors or indication of what the issue was. Let me know if I can provide any more info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant