Skip to content

Commit

Permalink
Merge pull request #47 from Asana/handle-402
Browse files Browse the repository at this point in the history
Handle 402/403's with current API setup.
  • Loading branch information
theaeolianmachine authored Feb 9, 2018
2 parents 1ac7691 + 4c04419 commit 12dd8ad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Asana/Errors/AsanaError.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class AsanaError extends \Exception
{
const PREMIUM_ONLY_STR = "not available for free";
public function __construct($message, $status, $response)
{
$this->message = $message;
Expand All @@ -23,6 +24,16 @@ public static function handleErrorResponse($response)
{
switch ($response->code) {
case ForbiddenError::STATUS:
// Handle 403's with a premium response
try {
foreach ($response->body->errors as $error) {
if (strpos($error->message, self::PREMIUM_ONLY_STR) !== false) {
throw new PremiumOnlyError($response);
}
}
} catch (Exception $e) {
throw new ForbiddenError($response);
}
throw new ForbiddenError($response);
case InvalidRequestError::STATUS:
throw new InvalidRequestError($response);
Expand All @@ -32,6 +43,8 @@ public static function handleErrorResponse($response)
throw new NoAuthorizationError($response);
case NotFoundError::STATUS:
throw new NotFoundError($response);
case PremiumOnlyError::STATUS:
throw new PremiumOnlyError($response);
case RateLimitEnforcedError::STATUS:
throw new RateLimitEnforcedError($response);
case ServerError::STATUS:
Expand Down
16 changes: 16 additions & 0 deletions src/Asana/Errors/PremiumOnlyError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Asana\Errors;

use Asana\Errors\AsanaError;

class PremiumOnlyError extends AsanaError
{
const MESSAGE = 'Payment Required';
const STATUS = 402;

public function __construct($response)
{
parent::__construct(self::MESSAGE, self::STATUS, $response);
}
}

0 comments on commit 12dd8ad

Please sign in to comment.