setup handler exception

This commit is contained in:
Muhammad Fajar
2022-10-28 11:13:56 +07:00
parent 302cc343fd
commit 03fdf2684d
3 changed files with 152 additions and 2 deletions

View File

@@ -0,0 +1,90 @@
<?php
namespace Modules\Linksehat\Http\Controllers\Api;
use App\Models\PractitionerRole;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class SpecialityController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request)
{
$organizationId = $request->organization_id;
$specialityId = $request->speciality_id;
if (empty($organizationId) || empty($specialityId)) {
$messageorganizationId = !empty($organizationId) ? ' ' : ' organization_id or ';
$messageSpecialityId = !empty($specialityId) ? ' ' : 'speciality_id';
abort(400, 'missing parameter' . $messageorganizationId . $messageSpecialityId);
}
return PractitionerRole::query()->with('practitioner')->where('organization_id', $organizationId)->where('speciality_id', $specialityId)->get();
}
/**
* Show the form for creating a new resource.
* @return Renderable
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
* @param Request $request
* @return Renderable
*/
public function store(Request $request)
{
//
}
/**
* Show the specified resource.
* @param int $id
* @return Renderable
*/
public function show(Request $request, $id)
{
//
}
/**
* Show the form for editing the specified resource.
* @param int $id
* @return Renderable
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
* @param Request $request
* @param int $id
* @return Renderable
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
* @param int $id
* @return Renderable
*/
public function destroy($id)
{
//
}
}

View File

@@ -6,6 +6,7 @@ use Modules\Linksehat\Http\Controllers\Api\DashboardController;
use Modules\Linksehat\Http\Controllers\Api\DoctorController;
use Modules\Linksehat\Http\Controllers\Api\HospitalController;
use Modules\Linksehat\Http\Controllers\Api\ProfileController;
use Modules\Linksehat\Http\Controllers\Api\SpecialityController;
/*
|--------------------------------------------------------------------------
@@ -29,8 +30,7 @@ Route::prefix('linksehat')->group(function () {
Route::get('social-login/{provider}', [AuthController::class, 'redirectSocialLogin']);
Route::get('social-login/{provider}/callback', [AuthController::class, 'handleSocialLoginCallback']);
// Route::get('articles', [ArticleController::class, 'index']);
// Route::get('articles/id', [ArticleController::class, 'show']);
Route::get('specialities', [SpecialityController::class, 'index']);
Route::get('hospitals', [HospitalController::class, 'index']);
Route::get('hospitals/{id}', [HospitalController::class, 'show']);

View File

@@ -2,7 +2,12 @@
namespace App\Exceptions;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
class Handler extends ExceptionHandler
@@ -47,4 +52,59 @@ class Handler extends ExceptionHandler
//
});
}
public function render($request, Throwable $exception)
{
if ($request->wantsJson()) {
if ($exception instanceof AuthenticationException) {
return response()->json([
'status' => 'error',
'message' => 'Unauthenticated',
'errors' => [
'Unauthenticated'
]
], 401);
}
if ($exception instanceof AuthorizationException) {
return response()->json([
'status' => 'error',
'message' => 'This action is unauthorized.',
'errors' => [
'This action is unauthorized.'
]
], 403);
}
if ($exception instanceof NotFoundHttpException) {
return response()->json([
'status' => 'error',
'message' => 'Route Not Found',
'errors' => [
'Route Not Found'
]
], 404);
}
if ($exception instanceof ModelNotFoundException) {
return response()->json([
'status' => 'error',
'message' => 'Record for ' . str_replace('App', '', str_replace('\\Models\\', '', $exception->getModel())) . ' not found',
'errors' => [
'Record for ' . str_replace('App', '', str_replace('\\Models\\', '', $exception->getModel())) . ' not found'
]
], 404);
}
if ($exception instanceof ValidationException) {
return response()->json([
'status' => 'error',
'message' => 'The given data was invalid.',
'errors' => collect($exception->errors())->flatten()
], 422);
}
}
return parent::render($request, $exception);
}
}