fastruby / harvesting

Ruby wrapper for the Harvest API v2
MIT License
28 stars 30 forks source link

Add RequestNotFound error #54

Closed JuannFerrari closed 4 years ago

JuannFerrari commented 4 years ago

Hi! :wave: I leave a small contribution here, thank you for developing this gem. This error happened to me in a system I'm working on and I use a workaround to handle it, but I thought it might be useful if I fix the error directly in the gem, hope you like it!

refers to https://github.com/fastruby/harvesting/issues/55


What changes does this PR include?

Description

When a user tries to create a new assignment in a project the gem does the next:

POST /v2/projects/{PROJECT_ID}/user_assignments

The problem is that the PROJECT_ID could be nil (in the worst-case scenario), so the response of Harvest API is an HTML body (in a string) with 404 HTTP code.

Screen Shot 2020-08-21 at 17 36 14

Harvesting tries to parse this HTML as a JSON and fails

    def create(entity)
      url = "#{DEFAULT_HOST}/#{entity.path}"
      uri = URI(url)
      response = http_response(:post, uri, body: entity.to_hash)
      entity.attributes = JSON.parse(response.body) #### this line
      entity
    end

This raises a JSONParse::Error because is trying to parse an HTML response, and this error is not being rescued anywhere and handling it in a proper way. This PR fixes that situation :)

Summary

JuannFerrari commented 4 years ago

Hey @etagwerker

I totally agree with your suggestions, I have already made the changes.

Thanks!