Larval 5.1:尝试使用SOAP API源捕获不捕获自定义异常的catch块

时间:2017-05-25 05:28:04

标签: php laravel soap laravel-5

我们已经创建了一个连接到客户的SOAP api系统的API。我们有一个自定义异常CustomerNotFound:

<?php

namespace App\Exceptions;


class CustomerNotFoundException extends  \Exception
{
    protected $code = 'customer_not_found';
    protected $message = 'Customer not found!';
}

用于检索用户信息的Soap类功能

public function getCustomerById($customerID)
    {

        $parameters = $this->appendToParameters([
            'customerID' => $customerID])
            ->getParameters();

        $result = $this->client->GetCustomerByID($parameters);

        if ($result->SDKResult->ResultCode === 'Success') {
            $data = $result->GetCustomerByIDResult;

            return $data;
        }

        throw new CustomerNotFoundException();

    }

调用soap类函数的控制器:

public function getCustomerDetails(Request $request, $customerId)
    {
        try {
            $customerDetails = $this->customerService->getCustomerById($customerId);

            return $this->respond($customerDetails);

        } catch (\SoapFault $e) {
            $this->respondInternalError($e->getMessage());
        } catch (\App\Exceptions\CustomerNotFoundException $c) {
             $this->respondNotFound($c->getMessage());
        }
}

我知道异常是有效的,因为当在另一个控制器上调用soap函数时,它会工作,然后在给出无效的客户ID时正确捕获异常。此外,当我删除try catch块并使用无效id调用控制器函数时,api返回CustomerNotFound异常。但是当它在try catch块中时,它只返回一个STATUS 200和一个空响应(没有json等)。没有显示错误。有什么想法吗?

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Response;
use JWTAuth;


class ApiController extends Controller 
{

    protected $statusCode = 200;


    public function isDevMode(){
        return request()->get('__devmode__', null) == 'ph';
    }

    public  function  err($msg, $code = 'error'){
        return $this->respond([
            'data'=> null,
            'status'=> [
                'code'=> $code,
                'msg'=> $msg,
                'success'=> false
            ]
        ]);
    }

    public function ok($data =[],$msg='Success',$code='success'){
        return $this->respond([
            'data'=> $data,
            'status'=> [
                'code'=> $code,
                'msg'=> $msg,
                'success'=> true
            ]
        ]);
    }

    public function getStatusCode()
    {
        return $this->statusCode;
    }

    public function setStatusCode($statusCode)
    {
        $this->statusCode = $statusCode;

        return $this;
    }

    public function respondNotFound($message = 'Not Found.',$code='not_found')
    {

        return $this->setStatusCode(404)->respondWithError($message,$code);
    }

    public function respondInternalError($message = 'Internal Server Error.', $code='internal_server_error')
    {

        return $this->setStatusCode(500)->respondWithError($message,$code);
    }

    public function respond($data, $headers = ['Content-type'=> 'application/json; charset=utf-8'])
    {

        return Response::json($data, $this->getStatusCode(), $headers);
    }

    public function respondWithError($message, $code = 'error')
    {

        return $this->respond([
            'error' => [
                'code' => $code,
                'message' => $message
                // 'status_code' => $this->getStatusCode()
            ]
        ]);
    }

    public function getUserTokenDetails()
    {
        $token = JWTAuth::getToken()->get();
        try {

        if (! $user = JWTAuth::parseToken()) 
        {
            return response()->json(['user_not_found'], 404);
        }
        } 
        catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) 
        {
            $this->setStatusCode(401);
            return $this->respondWithError('Token has expired.','token_expired');
        } 
        catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) 
        {
            $this->setStatusCode(401);
            return $this->respondWithError('Token is invalid','token_invalid');
        } 
        catch (Tymon\JWTAuth\Exceptions\JWTException $e) 
        {
            $this->setStatusCode(401);
            return $this->respondWithError('Token not found.','token_not_found');
        }
        $payload = JWTAuth::parseToken()->getPayload();
        $user = $payload['customClaims']; 
        return $user['CUST_ID'];
    }

}

0 个答案:

没有答案