Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error handling #146

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions src/Libilsws.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,37 @@ class APIException extends Exception
{

// Handles API errors that should be logged
public function errorMessage ($error = "", $code = 0)
public function errorMessage ($error = '', $code = 0)
{
$message = '';
$err_message = json_decode($error, true);
if ( json_last_error() === JSON_ERROR_NONE ) {
if ( !empty($err_message['messageList'][0]['message']) ) {
$error = $err_message['messageList'][0]['message'];
}
}

switch ($code) {
case 400:
$message .= "HTTP $code: Bad Request";
$message = "HTTP $code: Bad Request";
break;
case 401:
$message .= "HTTP $code: Unauthorized";
$message = "HTTP $code: Unauthorized";
break;
case 403:
$message .= "HTTP $code: Forbidden";
$message = "HTTP $code: Forbidden";
break;
case 404:
$message .= "HTTP $code: Not Found";
$message = "HTTP $code: Not Found";
break;
case 500:
$message .= "HTTP $code: Internal Server Error";
case (preg_match('/^5\d\d$/', $code) ? true : false):
$message = "HTTP $code: SirsiDynix Web Services unavailable";
break;
default:
$message = "HTTP $code: $error";
}

$err_message = json_decode($error, true);
if ( json_last_error() === JSON_ERROR_NONE ) {
if ( !empty($err_message['messageList'][0]['message']) ) {
$error = $err_message['messageList'][0]['message'];
$message .= ": $error";
}
}

if ( !$message ) {
$message = "HTTP $code: $error";
}

return $message;
return $message;
}
}

Expand Down
Loading