我使用的是Stripe API,下面的代码行用于检查客户:
$StripeCustomer = \Stripe\Customer::retrieve($cust_id);
如果未找到客户ID /客户ID不存在,则返回错误。那时我只想创建一个客户ID。 但是如何捕获并评估错误?关于捕获卡错误,SO上有多种答案,但是它们不能像GET请求那样起作用。
Laravels错误处理程序确实返回错误。看起来像这样:
在ApiRequestor.php第181行中在ApiRequestor :: _ specificAPIError('{“ error”:{“ code”:“ resource_missing”,“ doc_url”:“ https://stripe.com/docs/error-codes/resource-missing”,“ message”:“没有这样的客户:cus_CHDZD223OmY75y”,“ param”:“ id“,” type“:” invalid_request_error“}}','404',array('Server'=>'nginx','Date'=>'星期一,2018年8月13日11:55:49 GMT','内容-Type'=>'application / json','Content-Length'=>'234','Connection'=>'keep-alive','Access-Control-Allow-Credentials'=>'true','访问-Control-Allow-Methods'=>'获取,发布,标题,选项,删除','Access-Control-Allow-Origin'=>'*','Access-Control-Expose-Headers'=>'请求- Id,Stripe-Manage-Version,X-Stripe-External-Auth-Required,X-Stripe-Privileged-Session-Required,'Access-Control-Max-Age'=>'300','Cache-Control'= >'无缓存,无存储','Request-Id'=>'req_0iY8NIWwT0tAqr','Stripe-Version'=>'2018-07-27','Strict-Transport-Security'=>'最大年龄= 31556926; includeSubDomains; preload'),array('error'=> array('code'=>'resource_missing','doc_url'=>'https://stripe.com/docs/error-codes/resource-missing','message'=>'没有这样的需求omer:cus_CHDZD223OmY75y','param'=>'id','type'=>'invalid_request_error')),array('code'=>'resource_missing','doc_url'=>'https://stripe.com/docs/error-codes/resource-missing',' message'=>'没有这样的客户:cus_CHDZD223OmY75y','param'=>'id','type'=>'invalid_request_error'))在ApiRequestor.php第144行中
但是我需要捕获错误并进行检查。任何帮助表示赞赏。
到目前为止已尝试
我尝试了以下try / catch的变体:
try{
$StripeCustomer = \Stripe\Customer::retrieve($cust_id);
} catch (Exception $e){
return "no customer found";
}
答案 0 :(得分:2)
您的代码看起来通常是正确的,您确实应该在检索调用周围使用try / catch块。我建议查看从Stripe返回的响应代码,以确定确切的错误是否表明该客户不存在,或者是否为另一种错误。这是一个示例:
np.greater(A, B[:,np.newaxis])
array([[ True, False, True, True],
[False, False, False, True]], dtype=bool)
您可以在docs中阅读有关处理条带错误的更多信息。