简体   繁体   English

如何将异常文本从php传输到javascript

[英]How can I transfer exception text from php to javascript

I have a file download mechanism with axios and laravel. 我有axios和laravel的文件下载机制。 From the side of php I have several situations where I have to throw exceptions. 从php的一侧,我有几种情况必须抛出异常。

for example 例如

    $result = file_put_contents($filepath, $filedata);

    if($result){

        $photoModer = new PhotoModeration();
        $photoModer->newUserUpload($filepath);


        return true;
    }else{
        throw new Exceptions\FileUploadException('Can not upload file');
    }

In my controller I pass the data to the side js with json array 在我的控制器中,我将数据传递给带有json数组的侧面js

       if($resultUpload === true){

       $json_array = [
           'status' => 'success',
           'message' => 'Success'
       ];

   }else{

       $json_array = [
           'status' => 'error',
           'message' => 'Error'
       ];

   }

    return json_encode($json_array);

then in js I just use result.text . 然后在js中,我只使用result.text My question is in the following. 我的问题在下面。 Is it possible, in any way, to pass the text from exception to the side js? 是否有可能以任何方式将文本从异常传递到侧面js?

As @J.Meijer mentioned, you can use try ... catch statement. 如@ J.Meijer所述,您可以使用try ... catch语句。 so your code will be: 因此您的代码将是:

try (file_put_contents($filepath, $filedata)){

    $photoModer = new PhotoModeration();
    $photoModer->newUserUpload($filepath);

    return true;
} catch (Exception $e) {
    return json_encode($e);
}

I didn't test this code so that this only give you an idea. 我没有测试此代码,因此只给您一个想法。

In Laravel this mechanism already exists 在Laravel中这种机制已经存在

https://laravel.com/docs/5.5/errors#render-method https://laravel.com/docs/5.5/errors#render-method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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