Skip to content

Webhook Migration 1.2.x to 1.3.0

Anthony Persaud edited this page Jul 3, 2016 · 1 revision

Migrating from Parse Webhooks 1.2.x to 1.3.0.

As the Changes.md and Readme.md detail, we have changed the error handling system and payload processing in the Parse::Webhooks class. Here is a short migration guide to what has changed and how you should update your code for the new version.

  1. Change all exception raise "...msg..." with error!("...msg...").
  2. Remove the use of payload as an incoming argument and use methods directly.
  3. Verify if you need to additional handle exceptions.

1) Exception Handling

Before you would return an error to Parse from a cloud hook or function by raising an exception. In order to support the cases where you want to catch internal system errors in the stack by other components, we now require that you explicitly call error!() method to return a Parse error to the client.

If you code looked like this before:

class Artist < Parse::Object  
  # before_save trigger example
  webhook :before_save do
        raise "My error message" if user.nil?
       # ... do stuff ... 
  end
end

# cloud function example
Parse::Webhooks.route :function, "helloWorld" do
  raise "No parameters!" if params.empty?
  # ... do stuff ...
end

it should now be:

class Artist < Parse::Object  
  # before_save trigger example
  webhook :before_save do
        error!("My error message") if user.nil?
       # ... do stuff ... 
  end
end

# cloud function example
Parse::Webhooks.route :function, "helloWorld" do
  error!("No parameters!") if params.empty?
  # ... do stuff ...
end

2) Payload Parameter Changes

The block in which Cloud Code functions and triggers/hooks occur are now binded to the Parse::Payload object. If your code looked like this before:

class Artist < Parse::Object  
  # before_save trigger example
  webhook :before_save do |payload|
     artist = payload.parse_object
     the_user = payload.user
     # .. do stuff ...
     artist.update_payload
  end
end

# cloud function example
Parse::Webhooks.route :function, "helloWorld" do |payload|
  my_param = payload.params["my_param"]
  the_user = payload.user
  # ... do stuff ...
end

you can now discard the payload parameter and access methods directly:

class Artist < Parse::Object  
  # before_save trigger example
  webhook :before_save do
     artist = parse_object
     the_user = user
     # .. do stuff ...
     true
  end
end

# cloud function example
Parse::Webhooks.route :function, "helloWorld" do
  my_param = params["my_param"]
  the_user = user
  # ... do stuff ...
end

3) Handle additional exceptions if needed. (Optional)

For specific Parse and HTTP status error codes, we will raise an exception. This in conjunction with the new save! method, will better debugging of your applications (ex. using Sentry) and allow systems like Sidekiq to retry any workers when specific exceptions are raised in whose task can be retried at a later time. The list of new exceptions is as follows:

  • Parse::RequestLimitExceededError - occurs when you have exceeded your request limit on Parse. This is usually Parse error 155.
  • Parse::AuthenticationError - occurs when you have invalid credentials. This is usually returned by Parse as HTTP status code of 401 and 403.
  • Parse::TimeoutError - occurs on a net/http timeout request. This is usually error code 124 (Parse) or 143 (net/http layer).
  • Parse::ProtocolError - occurs when there is an issue with the request. This is usually HTTP status code 405 and 406.
  • Parse::ConnectionError - occurs when the API request could not be fulfilled (API issue or failed network connection).
  • Parse::ServerError - occurs in all other cases when there is a critical error. This is usually any time Parse server returns an error code of <=100 or a HTTP status code of 500.