1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| namespace app\helpers\web;
use yii; use yii\base\Action; use yii\base\Exception; use yii\base\UserException; use yii\web\HttpException;
class ErrorAction extends \yii\web\ErrorAction { const URL = '{host}/api/v3/projects/{project_id}/issues'; // host替换为主机地址, project_id为项目id const PRIVATE_TOKEN = 'tD3Te-ctECeGwEHH7-ec'; const ASSIGNEE_ID = 21;
public function run() { if (($exception = Yii::$app->getErrorHandler()->exception) === null) { $exception = new HttpException(404, Yii::t('yii', 'Page not found.')); }
if ($exception instanceof HttpException) { $code = $exception->statusCode; } else { $code = $exception->getCode(); } if ($exception instanceof Exception) { $name = $exception->getName(); } else { $name = $this->defaultName ?: Yii::t('yii', 'Error'); } $preCode = $code; if ($code) { $name .= " (#$code)"; }
if ($exception instanceof UserException) { $message = $exception->getMessage(); } else { $message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.'); } if ($code != '404') { //自动向GitLab提交Bug $url = self::URL; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'PRIVATE-TOKEN: '.self::PRIVATE_TOKEN, ));
curl_setopt($ch, CURLOPT_POSTFIELDS, [ 'title' => $message, 'description' => '<blockquote>'.Yii::$app->request->getReferrer().'</blockquote>'. '<blockquote>' . Yii::$app->request->absoluteUrl . '</blockquote><br/><pre>' . $exception . '</pre>', 'assignee_id' => self::ASSIGNEE_ID, 'labels' => '捕虫器,' . $name, ]); curl_setopt($ch, CURLOPT_HEADER, false); // Pass TRUE or 1 if you want to wait for and catch the response against the request made curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // For Debug mode; shows up any error encountered during the operation curl_setopt($ch, CURLOPT_VERBOSE, false); $response = curl_exec($ch); curl_close($ch); } if (Yii::$app->getRequest()->getIsAjax() || strpos($_SERVER['REQUEST_URI'], '/api/') > -1) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return [ 'message' => $message ]; } else { return $this->controller->render($this->view ?: $this->id, [ 'name' => $name, 'message' => $message, 'exception' => $exception, ]); } } }
|