简体   繁体   中英

Function not returning anything when using switch case

If I return an array before SWITCH condition it works fine but if try to return something after SWITCH condition (even hardcoded array) it does not return anything. Also it does not go in any CASE , not even DEFAULT . Even print or echo does not work.

My $e->getAwsErrorCode() function is returning InvalidSignatureException but it is not entering into related switch case.

I checked error log and there is nothing there, no error or warning printed on the page.

private function rekognition_error_catch($e)
    {
        $arr_error = array();
        /*return [
            'error_code' => 34,
            'error_message' => 'Error'
        ];*/
        switch ($e->getAwsErrorCode()) {
            case 'InvalidParameterException':
                $arr_error['error_code'] = 71;
                $arr_error['error_message'] = $e->getAwsErrorMessage();
                break;
            case 'InvalidS3ObjectException':
                $arr_error['error_code'] = 72;
                $arr_error['error_message'] = $e->getAwsErrorMessage();
                break;
            case 'ImageTooLargeException':
                $arr_error['error_code'] = 73;
                $arr_error['error_message'] = $e->getAwsErrorMessage();
                break;
            case 'AccessDeniedException':
                $arr_error['error_code'] = 74;
                $arr_error['error_message'] = $e->getAwsErrorMessage();
                break;
            case 'InternalServerError':
                $arr_error['error_code'] = 75;
                $arr_error['error_message'] = $e->getAwsErrorMessage();
                break;
            case 'ThrottlingException':
                $arr_error['error_code'] = 76;
                $arr_error['error_message'] = $e->getAwsErrorMessage();
                break;
            case 'ProvisionedThroughputExceededException':
                $arr_error['error_code'] = 77;
                $arr_error['error_message'] = $e->getAwsErrorMessage();
                break;
            case 'InvalidImageFormatException':
                $arr_error['error_code'] = 78;
                $arr_error['error_message'] = $e->getAwsErrorMessage();
                break;
            case 'InvalidSignatureException': 
                $arr_error['error_code'] = 79;
                $arr_error['error_message'] = $e->getAwsErrorMessage();
                echo '1';
                print_r($arr_error);
                break;
            default:
                //throw new Exception($e->getAwsErrorMessage(),80);
                $arr_error['error_code'] = 80;
                $arr_error['error_message'] = $e->getAwsErrorMessage();
        }
        echo '2';
        print_r($arr_error);
        return [
          'error_code' => 34,
          'error_message' => 'Error'
        ];
    }

Your cases set $arr_array , but never return it anywhere, you always return a default value of [ 'error_code' => 34, 'error_message' => 'Error' ] , therefore, you need to check if values in that array are set, so;

if (!empty($arr_error['error_code']))
{
    return $arr_error;
}
return [
  'error_code' => 34,
  'error_message' => 'Error'
];

The above checks to see if the error code is set in the $arr_error and if it is, will use this, otherwise, your default 34 error is returned

Just as a point of note, you call $e->getAwsErrorMessage() an awful lot, storing this will make it a little simpler, so for example;

$arr_error = array();
$err_message = $e->getAwsErrorCode();
switch ($err_message) {
    case 'InvalidParameterException':
        $arr_error['error_code'] = 71;
        $arr_error['error_message'] = $err_message;
        break;
    /* Your other code, just using $err_message instead of $e->getAwsErrorCode()
    This will save some overhead from the function calls */
}

如果您的切换案例当时没有返回任何值,则必须将默认案例放入您的切换案例中。默认情况是在任何其他案例都不匹配时执行的案例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM