简体   繁体   中英

Generating HTTP errors using Fat-Free-Framework

I am using fat-free framework for my Rest API. I have a unique constraint on one of my database field. My question is how can i trigger a proper HTTP error based on mysql duplicate entry error.

Following is my insert function

    $username = $f3->get('username');
    $status=$GLOBALS['db']->exec('INSERT INTO Users 
                     VALUES(:userID,:username)',
                     array(':userID'=>'',':username'=>$username));

    header('Content-Type: application/json');                                                                   
    return (json_encode($status)); 

My insert statement triggers following error based on unique constraint on field

Internal Server Error
PDOStatement: Duplicate entry 'someUserName' for key 'username'

I want to trigger HTTP error because on the client side i can handle HTTP errors. Please guide me.

您可以使用 try / catch 语句并调用$f3->error('400','MSG')发送您最喜欢的 http 状态代码之一并触发 ONERROR 处理程序,如果您只想发送状态代码标头,你可以使用$f3->status方法

尝试查看此文档: http ://fatfreeframework.com/framework-variables#about-the-f3-error-handler

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