Skip to content

Commit

Permalink
Update: Add Support Folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Ph.Hitachi authored and Ph.Hitachi committed Aug 13, 2023
1 parent 64b8019 commit a134e1a
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
94 changes: 94 additions & 0 deletions app/Support/Exceptions/GraphApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Support\Exceptions;

use Exception;
use Throwable;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class GraphApiException extends Exception
{
/**
* The error type associated with the exception.
*
* @var string
*/
protected $errorType;

/**
* The error subcode associated with the exception.
*
* @var mixed
*/
protected $errorSubcode;

/**
* Create a new GraphApiException instance.
*
* @param string|null $message The exception message.
* @param mixed|null $code The error code or subcode.
* @param int $statusCode The HTTP status code.
* @param Throwable|null $previous The previous exception if available.
*/
public function __construct($message = null, $code = null, $statusCode = Response::HTTP_BAD_REQUEST, Throwable $previous = null)
{
$this->errorSubcode = $code;
$this->errorType = basename(str_replace('\\', '/', get_class($this)));

parent::__construct($message, $statusCode, $previous);
}

/**
* Report or log the exception.
*
* You can log or report the exception here if needed.
*/
public function report()
{
// Log or report the exception if necessary
}

/**
* Render the exception into a JSON response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse The JSON response with formatted error.
*/
public function render($request): JsonResponse
{
return new JsonResponse(
data: $this->getFormattedError(),
status: $this->getCode()
);
}

/**
* Get the formatted error response.
*
* @return array The formatted error response array.
*/
protected function getFormattedError(): array
{
return [
'error' => [
'message' => $this->getMessage(),
'type' => $this->errorType,
'code' => $this->errorSubcode ?? 'fatal_error',
'trace_id' => $this->generateID(),
],
];
}

/**
* Generate a random trace ID.
*
* @return string The generated trace ID.
*/
public function generateID(): string
{
return substr(str_shuffle(
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"
), 0, 16);
}
}
44 changes: 44 additions & 0 deletions app/Support/Exceptions/OAuthException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Support\Exceptions;

use App\Support\Exceptions\GraphApiException;
use Symfony\Component\HttpFoundation\Response;

class OAuthException extends GraphApiException
{
/**
* Localization key prefix for error messages.
*
* @var string
*/
protected $localize = "auth";

/**
* Create a new OAuthException instance.
*
* @param string|null $message The exception message.
* @param string|null $code The error code.
* @param array $data Additional data for translation.
* @param int|null $statusCode The HTTP status code.
*/
public function __construct(
string $message = null,
string $code = null,
array $data = [],
Response|int $statusCode = Response::HTTP_BAD_REQUEST
){
// Call the parent constructor with necessary parameters.
parent::__construct(
code: $code,
message: $message ?? trans("$this->localize.$code", $data), // Use localization for the message.
statusCode: $statusCode, // Default status code if not provided.
);
}

public function report()
{
// Log or report the exception if necessary
}
}

20 changes: 20 additions & 0 deletions app/Support/Traits/Authenticatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace App\Support\Traits;

use App\Models\User;
use Illuminate\Http\JsonResponse;

trait Authenticatable
{
public function responseWithToken(string $access_token, User $user = null)
{
return new JsonResponse([
'user' => $user ?: auth()->user(),
'authorization' => [
'access_token' => $access_token,
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * 60,
],
]);
}
}

0 comments on commit a134e1a

Please sign in to comment.