Skip to content

Commit

Permalink
When attempting to create pad, bail if a 4xx response is returned
Browse files Browse the repository at this point in the history
This technique prevents infinite loops when an unexpected result is returned
by requests to Etherpad Lite, such as for an overlong request URI.

See #23
  • Loading branch information
boonebgorges committed Jan 23, 2013
1 parent abe874f commit b0d0ec3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion includes/class-participad-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ protected function call( $function, array $arguments = array() ) {
throw $e;
}

if ( 200 !== $request['response']['code'] ) {
throw new UnexpectedValueException( "Unknown error: " . $request['response']['code'] );
}

$result = json_decode( $request['body'] );

if ( $result === null ) {
throw new UnexpectedValueException("JSON response could not be decoded");
throw new UnexpectedValueException( "JSON response could not be decoded" );
}

return $this->handleResult($result);
Expand Down
19 changes: 18 additions & 1 deletion includes/class-participad-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,24 @@ public static function create_ep_post( $wp_post_id, $ep_post_group_id ) {
$ep_post = participad_client()->createGroupPad( $ep_post_group_id, $ep_post_id, $wp_post_content );
$pad_created = true;
} catch ( Exception $e ) {
$ep_post_id = self::generate_random_name();

$error_message = $e->getMessage();
$error_code = substr( $error_message, strrpos( $error_message, ':' ) + 2 );

switch ( $error_code ) {

// Request URI too long
// @see https://github.com/boonebgorges/participad/issues/23
case '414' :
return new WP_Error( 'create_ep_post', __( 'Could not create the Etherpad Lite post', 'participad' ) );
break;

// Assume that there's a conflict, and try again
default :
$ep_post_id = self::generate_random_name();
break;

}
}
}

Expand Down

0 comments on commit b0d0ec3

Please sign in to comment.