Tinywan / webman-exception

exception handler library for webman plugin
https://www.workerman.net/webman
MIT License
13 stars 6 forks source link

自定义配置返回的code码的时候报错了 #18

Open new123-ui opened 1 month ago

new123-ui commented 1 month ago

希望可以实现自定义配置返回code值,不是配置statusCode,而不是在app里面配置的固定的,代码如下 throw new BadRequestHttpException('帖子未找到,无法操作' , [ 'errorCode' => ApiSignExceptionCode::ARTICLE_NOT_FOUND, ]); 现阶段操作修改的逻辑为 image

new123-ui commented 1 month ago
protected function buildResponse(): Response
{
    $bodyKey = array_keys($this->config['body']);
    $bodyValue = array_values($this->config['body']);
    $responseBody = [
        $bodyKey[0] ?? 'code' => $this->setCode($bodyValue, $this->errorCode), // 自定义异常code码
        $bodyKey[1] ?? 'message' => $this->errorMessage,
        $bodyKey[2] ?? 'data' => $this->responseData,
    ];

    $header = array_merge(['Content-Type' => 'application/json;charset=utf-8'], $this->header);
    return new Response($this->statusCode, $header, json_encode($responseBody));
}

private function setCode($bodyValue, $errorCode)
{
    if($errorCode > 0){
        return $errorCode;
    }
    return  $bodyValue[0] ?? 0;
}
Tinywan commented 1 month ago

欢迎提价PR哈

new123-ui commented 1 month ago

不好意思,最近在忙,家里的网络不行,好像还没有提交上,稍微等一下,我换个姿势再来提交一下 image

new123-ui commented 1 month ago

提交了 image

Tinywan commented 1 month ago

谢谢:https://github.com/Tinywan/webman-exception/releases/tag/v1.5.4