Skip to content

Commit

Permalink
Merge branch 'hotfix/2.0.6' into 'develop'
Browse files Browse the repository at this point in the history
tests completed

See merge request php/packages/api-exceptions!13
  • Loading branch information
Pooria Arab committed Jan 13, 2021
2 parents 3b9dfc3 + c5bdbbb commit edf5c85
Show file tree
Hide file tree
Showing 25 changed files with 797 additions and 287 deletions.
Empty file modified README.md
100644 → 100755
Empty file.
10 changes: 3 additions & 7 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"liateam/api-response": "^2.0"
"liateam/api-response": "2.*"
},
"repositories": {
"liateam/api-response" : {
Expand All @@ -20,12 +20,8 @@
},
"autoload": {
"psr-4": {
"Liateam\\ApiExceptions\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
"Liateam\\ApiExceptions\\": "src",
"Liateam\\ApiExceptions\\Tests\\" : "tests"
}
},
"extra": {
Expand Down
63 changes: 0 additions & 63 deletions src/Contracts/ApiException.php

This file was deleted.

69 changes: 69 additions & 0 deletions src/Contracts/ApiExceptionAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Liateam\ApiExceptions\Contracts;

use Exception;
use \Throwable;
use Liateam\ApiResponse\Responses\FailureResponse;
use Liateam\ApiResponse\Contracts\ResponseContract;

abstract class ApiExceptionAbstract extends Exception
{
/**
* @var Throwable
*/
protected $exception;


protected $errors = [];
/**
* @var FailureResponse
*/
private $failureResponse;

/**
* ApiException constructor.
* @param Throwable $exception
*/
public function __construct(Throwable $exception)
{
$this->exception = $exception;
parent::__construct($exception->getMessage(), $exception->getCode());
}

/**
* renders the error for api
*
* @return ResponseContract
*/
public function render()
{
$this->setCode($this->exception->getCode());
$this->setMessage($this->exception->getMessage());
$response = new FailureResponse($this->getCode(), $this->getMessage());
return $response
->setResponseKey('error')
->setResponseValue($this->getErrors())
->render();
}

public function setErrors($errors)
{
if (env('APP_DEBUG')) {
$this->errors= [
'trace' => $this->exception->getTrace(),
'line' => $this->exception->getLine(),
];
}
return $this;
}

/**
* @return array
*/
public function getErrors()
{
return $this->errors;
}

}
39 changes: 32 additions & 7 deletions src/Exceptions/CustomAuthenticationException.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,43 @@

use Throwable;
use Illuminate\Http\Response;
use Liateam\ApiExceptions\Contracts\ApiException;
use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract;

class CustomAuthenticationException extends ApiException
class CustomAuthenticationException extends ApiExceptionAbstract
{
/**
* @var Throwable $exception
*/
public $exception;

/**
* CustomAuthenticationException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
* @param $exception
*/

public function __construct(Throwable $exception)
{
$this->exception = $exception;
parent::__construct($exception);
}

/**
* @param $code
* @return $this|CustomAuthenticationException
*/
public function setCode($code = null): self
{
$this->code = ($code) ? $code : Response::HTTP_FORBIDDEN;
return $this;
}

/**
* @param $message
* @return $this|CustomAuthenticationException
*/
public function __construct($message = "", $code = Response::HTTP_UNAUTHORIZED, Throwable $previous = null)
public function setMessage($message = null): self
{
parent::__construct($message, $code, $previous);
$this->message = !empty($message) ? $message : 'Unauthenticated Exception';
return $this;
}
}
39 changes: 32 additions & 7 deletions src/Exceptions/CustomDefaultException.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,44 @@

namespace Liateam\ApiExceptions\Exceptions;

use Throwable;
use Illuminate\Http\Response;
use Liateam\ApiExceptions\Contracts\ApiException;
use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract;

class CustomDefaultException extends ApiException
class CustomDefaultException extends ApiExceptionAbstract
{
/**
* @var Throwable $exception
*/
public $exception;

/**
* CustomAuthenticationException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
* @param $exception
*/
public function __construct(Throwable $exception)
{
parent::__construct($exception);
$this->exception = $exception;
}

/**
* @param $code
* @return CustomDefaultException
*/
public function setCode($code = null): self
{
$this->code = $code ? $code : Response::HTTP_INTERNAL_SERVER_ERROR;
return $this;
}

/**
* @param $message
* @return CustomDefaultException
*/
public function __construct($message = "", $code = Response::HTTP_UNPROCESSABLE_ENTITY, Throwable $previous = null)
public function setMessage($message = null): self
{
parent::__construct($message, $code, $previous);
$this->message = !empty($message) ? $message : 'Whoops! something went wrong!';
return $this;
}
}
40 changes: 32 additions & 8 deletions src/Exceptions/CustomModelNotFoundException.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,42 @@

use Throwable;
use Illuminate\Http\Response;
use Liateam\ApiExceptions\Contracts\ApiException;
use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract;

class CustomModelNotFoundException extends ApiException
class CustomModelNotFoundException extends ApiExceptionAbstract
{
/**
* CustomModelNotFoundException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
* @var Throwable $exception
*/
public function __construct($message = "", $code = Response::HTTP_INTERNAL_SERVER_ERROR, Throwable $previous = null)
public $exception;

/**
* CustomAuthenticationException constructor.
* @param $exception
*/
public function __construct(Throwable $exception)
{
$this->exception = $exception;
parent::__construct($exception);
}

/**
* @param $code
* @return self
*/
public function setCode($code = null)
{
$this->code = $code ? $code : Response::HTTP_NOT_FOUND;
return $this;
}

/**
* @param $message
* @return string
*/
public function setMessage($message = null)
{
parent::__construct($message, $code, $previous);
$this->message = !empty($message) ? $message : 'Model Not Found';
return $this;
}
}
41 changes: 33 additions & 8 deletions src/Exceptions/CustomNotFoundHttpException.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,43 @@

use Throwable;
use Illuminate\Http\Response;
use Liateam\ApiExceptions\Contracts\ApiException;
use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract;

class CustomNotFoundHttpException extends ApiException
class CustomNotFoundHttpException extends ApiExceptionAbstract
{
/**
* CustomNotFoundHttpException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
* @var Throwable $exception
*/
public function __construct($message = "", $code = Response::HTTP_NOT_FOUND, Throwable $previous = null)
public $exception;

/**
* CustomAuthenticationException constructor.
* @param $exception
*/
public function __construct(Throwable $exception)
{
parent::__construct($exception);
$this->exception = $exception;
}

/**
* @param null $code
* @return CustomNotFoundHttpException
*/
public function setCode($code = null)
{

$this->code = $code ? $code : Response::HTTP_NOT_FOUND;
return $this;
}

/**
* @param null $message
* @return $this|CustomNotFoundHttpException
*/
public function setMessage($message = null)
{
parent::__construct($message, $code, $previous);
$this->message = !empty($message) ? $message : 'Not Found';
return $this;
}
}
Loading

0 comments on commit edf5c85

Please sign in to comment.