Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add more specific HTTP error codes

Before we were sending every error with status
code 500 and a custom message. But most of the
errors we send aren't internal server errors, but
are actually user errors.

That's why we branched the send_error() function
into 3:

- send_server_error() with code 500
- send_user_error() with code 406 (original is Not
Acceptable)
- send_forbidden_error() with code 403
  • Loading branch information...
commit 69f9cbb287348e53f16a9049893b6ff4b277ca4c 1 parent 180ef9a
@nb nb authored
Showing with 15 additions and 5 deletions.
  1. +15 −5 liveblog.php
View
20 liveblog.php
@@ -340,7 +340,7 @@ public static function ajax_insert_entry() {
$entry_content = isset( $_POST['entry_content'] ) ? $_POST['entry_content'] : '';
if ( empty( $post_id ) )
- self::send_error( __( 'Sorry, that post is not accepting Liveblog entries.', 'liveblog' ) );
+ self::send_user_error( __( 'Sorry, that post is not accepting Liveblog entries.', 'liveblog' ) );
// Get the current user
$user = wp_get_current_user();
@@ -365,7 +365,7 @@ public static function ajax_insert_entry() {
// Bail if comment could not be saved
if ( empty( $new_comment_id ) || is_wp_error( $new_comment_id ) )
- self::send_error( __( 'Error posting entry', 'liveblog' ) );
+ self::send_server_error( __( 'Error posting entry', 'liveblog' ) );
// Are we replacing an existing comment?
if ( !empty( $replaces_comment_id ) ) {
@@ -655,7 +655,7 @@ public static function current_user_can_edit_liveblog() {
*/
public static function ajax_current_user_can_edit_liveblog() {
if ( ! self::current_user_can_edit_liveblog() ) {
- self::send_error( __( "Cheatin', uh?", 'liveblog' ) );
+ self::send_forbidden_error( __( "Cheatin', uh?", 'liveblog' ) );
}
}
@@ -666,7 +666,7 @@ public static function ajax_current_user_can_edit_liveblog() {
*/
public static function ajax_check_nonce( $action = self::nonce_key ) {
if ( ! isset( $_REQUEST[ self::nonce_key ] ) || ! wp_verify_nonce( $_REQUEST[ self::nonce_key ], $action ) ) {
- self::send_error( __( 'Sorry, we could not authenticate you.', 'liveblog' ) );
+ self::send_forbidden_error( __( 'Sorry, we could not authenticate you.', 'liveblog' ) );
}
}
@@ -676,11 +676,21 @@ public static function ajax_check_nonce( $action = self::nonce_key ) {
* Send an error message
* @param type $message
*/
- private static function send_error( $message ) {
+ private static function send_server_error( $message ) {
self::status_header_with_message( 500, $message );
exit();
}
+ private static function send_user_error( $message ) {
+ self::status_header_with_message( 406, $message );
+ exit();
+ }
+
+ private static function send_forbidden_error( $message ) {
+ self::status_header_with_message( 403, $message );
+ exit();
+ }
+
/**
* Encode some data and echo it (possibly without cached headers)
*
Please sign in to comment.
Something went wrong with that request. Please try again.