Skip to content

Instantly share code, notes, and snippets.

@tperrelli
Last active November 19, 2015 15:46
Show Gist options
  • Save tperrelli/d36d717f9a1cc4d97810 to your computer and use it in GitHub Desktop.
Save tperrelli/d36d717f9a1cc4d97810 to your computer and use it in GitHub Desktop.
<?php
namespace App\Exceptions;
use RuntimeException;
class BadRequestException extends HttpException
{
/**
* Create a new exception instance.
*
* @param string $title
* @param array $messages
* @param \Exception $previous
* @return void
*/
public function __construct($title = 'Bad Request', array $messages = [], \Exception $previous = null)
{
parent::__construct(400, $title, $messages, $previous);
}
}
<?php
namespace App\Exceptions;
use Exception;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
HttpException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
return parent::render($request, $e);
}
}
<?php
namespace App\Exceptions;
use Symfony\Component\HttpKernel\Exception\HttpException as AbstractHttpException;
/**
* Check HTTP codes for better understanding http://php.net/manual/en/function.http-response-code.php
*/
abstract class HttpException extends AbstractHttpException
{
/**
* The underlying response instance.
*
* @var array $messages
*/
protected $messages;
/**
* Create a new exception instance.
*
* @param array $messages
* @param int $code
* @return void
*/
public function __construct($statusCode, $title = null, array $messages = [], \Exception $previous = null)
{
$this->messages = $messages;
parent::__construct($statusCode, $title, $previous);
}
/**
* Get the underlying messages instance.
*
* @return array
*/
public function getMessages()
{
return $this->messages;
}
}
<?php
namespace App\Exceptions;
use RuntimeException;
class InvalidArgumentException extends BadRequestException
{
/**
* Create a new exception instance.
*
* @param array $messages
* @return void
*/
public function __construct(array $messages = [])
{
parent::__construct('Invalid Argument', $messages);
}
}
<?php
namespace App\Validators;
use Illuminate\Validation\Validator;
use App\Exceptions\InvalidArgumentException;
trait Validates
{
/**
* Validate the given model with the given rules.
*
* @param array $model
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return void
*/
public function validateThrowingException(array $model, array $rules, array $messages = [], array $customAttributes = [])
{
$validator = $this->getValidationFactory()->make($model, $rules, $messages, $customAttributes);
if ($validator->fails()) {
$this->throwInvalidArgumentException($model, $validator);
}
}
/**
* Throw the failed validation exception.
*
* @param array $model
* @param Validator $validator
* @return void
*/
protected function throwInvalidArgumentException(array $model, $validator)
{
throw new InvalidArgumentException($this->formatValidationErrors($validator));
}
/**
* Format the validation errors to be returned.
*
* @param Validator $validator
* @return array
*/
protected function formatValidationErrors(Validator $validator)
{
return $validator->errors()->getMessages();
}
/**
* Get a validation factory instance.
*
* @return \Illuminate\Contracts\Validation\Factory
*/
protected function getValidationFactory()
{
return app('validator');
}
}
<?php namespace App\Exceptions;
use Exception;
use Illuminate\Http\Response;
use App\Exceptions\Handler as BaseExceptionHandler;
class WhoopsHandler extends BaseExceptionHandler {
/**
* Render an exception into a response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Symfony\Component\HttpFoundation\Response
*/
public function render($request, Exception $e) {
$whoops = new \Whoops\Run;
if ($request->ajax())
{
dd('asd');
$whoops->pushHandler(new \Whoops\Handler\JsonResponseHandler());
}
else
{
dd('asdss');
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
}
return new Response($whoops->handleException($e), $e->getStatusCode(), $e->getHeaders());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment