2016-11-01 15 views
1

スリム3フレームワークを使用している私の小さなアプリケーションでカスタムエラーハンドラを使用しようとしています。Slim 3でミドルウェアエラー処理を使用する

エラーメッセージでjsonを出力し、エラーの説明でメールを送信するために使用するエラークラスを作成しました。

namespace App\Handlers; 

use Psr\Http\Message\ServerRequestInterface as Request; 
use Psr\Http\Message\ResponseInterface as Response; 

class CustomErrorHandlerMiddleware 
{ 

    const MAIL = "[email protected]"; 

    public function __construct($message = "", $errorCode = null) { 

     $this->message = $message; 
     $this->code = ($errorCode != null) ? $errorCode : 500; 
     $this->MailError(); 

    } 

    public function __invoke(Request $request, Response $response, $next, $msg) 
    { 
     $errorArray = ["status" => "error", "message" => $this->message]; 

     $response->withStatus($this->code)->withHeader('Content-Type','application/json')->write(json_encode($errorArray)); 

     $response = $next($request, $response); 

     return $response; 
    } 

    public function MailError() { 

     $headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

     $body = "Error:\n"; 
     $body .= $this->message."\n\n"; 

     try { 
      mail(self::MAIL, "Error on localdashboards", $body, $headers); 

     } catch(Exception $e) { 
      echo $e->getMessage(); 
     } 

    } 

} 

私はこれを完全に間違っているかもしれませんが、特定のエラーが発生した場合、別のクラス内でこれを呼び出すことはできますか?

私はこのような何かしようとしています:

$delete = $this->db->database->delete("msn_ad_data", ["campaign_name" => $facebookData[0]["campaign_name"]]); 

    if($delete) { 

     $this->db->database->insert('msn_ad_data', [ 
      "campaign_id" => $facebookData[0]["campaign_id"], 
      "campaign_name" => $facebookData[0]["campaign_name"], 
      "impressions" => $facebookData[0]["impressions"], 
      "status" => $facebookData[1]["status"], 
      "pagename" => $facebookData[1]["bankarea"], 
      "type" => $facebookData[1]["platform"] 
     ]); 

    } else { 
     $this->app->add(new \App\Handlers\CustomErrorHandlerMiddleware("Could not save data")); 
    }´ 

をしかし、動作するようには思えません。

基本的に私は

[{ 
    "status": "error", 
    "message": "Could not save data" 
}] 

のように見え、また、同じ応答で私にメールを送って、私はブラウザに出力をすることができ、JSON、との応答を送信します。

答えて

2

ここで使用するミドルウェアは正しいものではありません。

class CustomException extends \Exception {} 

} else { 
    throw new CustomException("Could not save data"); 
} 

は、あなたが同様に例外を必要とするカスタムのErrorHandler

$c = new \Slim\Container(); 
$c['errorHandler'] = function ($c) { 
    return function ($request, $response, $exception) use ($c) { 
     if($exception instanceof CustomException) { 
      $message = $exception->getMessage(); // "could not save" 
      $errorCode = //$exception->getErrorCode(); when you want todo this add this as method to the exception. 
      // send mail 
      $errorArray = ["status" => "error", "message" => $message]; 

      return $response->withStatus($errorCode)->withHeader('Content-Type','application/json')->write(json_encode($errorArray)); 

     } 
     return $c['response']->withStatus(500) 
          ->withHeader('Content-Type', 'text/html') 
          ->write('Something went wrong!'); 
    }; 
}; 
$app = new \Slim\App($c); 

を追加:あなたは、他のメールを送信したり、例外をスローし、スリムのErrorHandlerの中でそれを処理すべきです

関連する問題