Skip to content

Commit 3d20020

Browse files
committed
Fully support throwing exceptions when errors occur
1 parent 8ffe947 commit 3d20020

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

example/exception.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
// Query non existing post so you can see how errors are handled.
3+
declare(strict_types=1);
4+
5+
namespace writeas;
6+
7+
require_once( "../lib/writeas.php" );
8+
9+
$context = new \writeas\Context();
10+
$post = new Post( $context );
11+
$post->get( "nonexisting" );
12+
13+
?>

example/getPost.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
$post = new Post( $context );
1010
$post->get( "0uw2v93hq709r" );
1111

12-
?>
12+
?>

lib/Context.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,24 @@ public function request( string $url, ?string $postdata = null, ?string $customR
6363
}
6464

6565
$output = curl_exec( $this->ch );
66+
$statusCode = curl_getinfo( $this->ch, CURLINFO_HTTP_CODE );
6667

6768
curl_close( $this->ch );
6869

70+
if ( (int) $statusCode >= 400 ) {
71+
throw new WAException( null, $statusCode );
72+
73+
return null;
74+
}
75+
6976
if ( $output !== false && !empty( $output ) ) {
7077
$js = @json_decode( $output );
7178
if ( !empty( $js ) ) {
79+
if ( isset( $js->code ) && isset( $js->error_msg ) ) {
80+
if ( (int) $js->code >= 400 ) {
81+
throw new WAException( null, (int) $js->code );
82+
}
83+
}
7284
return $js;
7385
}
7486

lib/WAException.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace writeas;
5+
6+
class WAException extends \Exception
7+
{
8+
protected $exceptions = array(
9+
400 => "Bad Request", // The request didn't provide the correct parameters, or JSON / form data was improperly formatted.
10+
401 => "Unauthorized", // No valid user token was given.
11+
403 => "Forbidden", // You attempted an action that you're not allowed to perform.
12+
404 => "Not Found", // The requested resource doesn't exist.
13+
405 => "Method Not Allowed",// The attempted method isn't supported.
14+
410 => "Gone", // The entity was unpublished, but may be back.
15+
429 => "Too Many Requests", // You're making too many requests, especially to the same resource.
16+
500 => "Server Error",
17+
502 => "Server Error",
18+
503 => "Server Error"
19+
);
20+
21+
function __construct( $message = null, $code = 0, Exception $previous = null )
22+
{
23+
if ( isset( $this->exceptions[ $code ] ) ) {
24+
$message = $this->exceptions[ $code ];
25+
}
26+
27+
parent::__construct($message, $code, $previous);
28+
}
29+
}
30+
?>

lib/writeas.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const DEFAULT_ENDPOINT = "https://write.as/api";
55
const TOR_ENDPOINT = "http://writeas7pm7rcdqg.onion/api";
66

7+
require_once( "WAException.php" );
78
require_once( "Context.php" );
89
require_once( "Post.php" );
910
require_once( "Collection.php" );

0 commit comments

Comments
 (0)