英文:
How do I do error handling for failes RestAPI calls? I want to display the error instead of getting Code 500 Errors (Laravel)
问题
我的Web应用程序通过WooCommerce商店的Rest API请求内容。用户应该设置自定义过滤器(例如,订单状态)。然而,由于坏参数或商店不可用等原因导致的Rest API调用失败会导致HTTP 500错误。相反,我想尝试API调用并将错误消息显示给用户。
我尝试将请求包装在try-catch块中,但应用程序在那之前崩溃。
示例代码:
public static function getOrders($key, $secret, $url, $status = "processing"){
$woocommerce = WCApi::getClient($key, $secret, $url);
$response = "";
try {
// 这行代码引发错误
$response = $status == "all" ? $woocommerce->get('orders') : $woocommerce->get('orders', ['status' => $status]);
} catch (Exception $e) {
$response = $e;
}
return $response;
}
当请求不存在的状态时,我会收到以下错误消息:
错误:无效参数:status [rest_invalid_param]
这是包含我的上述函数的WCApi类的堆栈跟踪:
堆栈跟踪的截图
英文:
My webapplication requests content from a WooCommerce Shop via it's Rest API. The user is meant to set custom filters (e.g. order status). However, failed Rest API calls, due to e.g. bad parameteres or the shop not being available, result in HTTP 500 Errors. Instead I want to try the API call and display the errormessages to the user.
I tried wrapping the request in a try catch block, but the application crashes way before that.
Example code:
public static function getOrders($key, $secret, $url, $status = "processing"){
$woocommerce = WCApi::getClient($key, $secret, $url);
$response = "";
try {
// This line causes the error
$response = $status == "all" ? $woocommerce->get('orders') : $woocommerce->get('orders', ['status' => $status]);
} catch (Exception $e) {
$response = $e;
}
return $response;
}
When a status is requested that does not exist, I get a
> Error: Invalid parameter(s): status [rest_invalid_param]
error. This is the stacktrace with the WCApi class being the class containing my function from above:
Screenshot of the stacktrace
答案1
得分: -1
在你的代码中,你已经使用try-catch块捕获了异常。但是,你将异常对象本身分配给了$response变量,这可能不是期望的行为。为了显示错误消息而不是异常对象,你可以按照以下方式修改你的代码:
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
public static function getOrders($key, $secret, $url, $status = "processing") {
$woocommerce = WCApi::getClient($key, $secret, $url);
try {
// 进行API调用
$response = $status == "all" ? $woocommerce->get('orders') : $woocommerce->get('orders', ['status' => $status]);
// 处理响应
// ...
return $response;
} catch (Exception $e) {
// 记录错误
Log::error($e->getMessage());
// 显示自定义错误消息
$errorMessage = '在检索订单时发生错误,请稍后重试。';
return response()->json(['error' => $errorMessage], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
在更新后的代码中:
- 如果API调用成功,响应将原样返回。
- 如果发生异常,它会被捕获,使用Laravel的Log外观进行记录,并返回一个带有自定义错误消息和状态码500(HTTP_INTERNAL_SERVER_ERROR)的JSON响应。
确保导入所需的类,并根据你的需求调整响应处理。
英文:
In your code, you are already catching the exception using a try-catch block. However, you are assigning the exception object itself to the $response variable, which may not be the desired behavior. To display the error message instead of the exception object, you can modify your code as follows:
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
public static function getOrders($key, $secret, $url, $status = "processing") { $woocommerce = WCApi::getClient($key, $secret, $url);
try {
// Make the API call
$response = $status == "all" ? $woocommerce->get('orders') : $woocommerce->get('orders', ['status' => $status]);
// Process the response
// ...
return $response;
} catch (Exception $e) {
// Log the error
Log::error($e->getMessage());
// Display a custom error message
$errorMessage = 'An error occurred while retrieving orders. Please try again later.';
return response()->json(['error' => $errorMessage], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
In the updated code:
If the API call is successful, the response is returned as-is.
If an exception occurs, it is caught, logged using Laravel's Log facade, and a JSON response with a custom error message and a status code of 500 (HTTP_INTERNAL_SERVER_ERROR) is returned.
Make sure to import the necessary classes and adjust the response handling based on your requirements.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论