Try Catch incorrect for value null in laravel - laravel

I am making a selection for api but when you do not have the contract informed you are returning an empty array [] but you should return contract not found.
I believe that the catch is in the wrong way, but I already did some other tests and it didn't work.
try {
if(empty($contratos)){
return response()->json(
$contratos,
200
);
}else{
return response()->json([
"message" => "Contract not found",
"code" => "CONTRACT_NOT_FOUND"
], 406
);
}
} catch (\Exception $e) {
if ($exception->getStatusCode() == 406){
return response()->json([
"message" => "Contrato não encontrado",
"code" => "CONTRACT_NOT_FOUND"
]);
}
}

You can check if there are no results from the query like this.
if ($contratos->isEmpty()) {
return response()->json([
"message" => "Contract not found",
"code" => "CONTRACT_NOT_FOUND"
], 406);
} else {
return response()->json($contratos, 200);
}

you should put a dd($exceptopm->getStatusCode()) in the exception block and see what is the status code returned, typically not found should be 404 instead of 406
you should also try to use a switch statement
switch($exception->getStatusCode()) {
case 400:
return [];
break;
case 404:
return [];
break;
default:
return [];
}

Related

Changing default error message when record not found - Laravel API

I am building a simple API, which there is a point that when the ID entered in the endpoint URL does not point to a valid record, I get a standard NotFoundHttpException. And I cannot figure out how to override this in order to provide my own error message response as I do not wish to share my Model name etc.
Endpoint
Route::get('{mrl}', [MrlController::class, 'show']);
Controller
public function show(Mrl $mrl)
{
if ($data = $mrl) {
return response(['status' => 'ok', 'data' => $data], 200);
} else {
return response(['status' => 'error', 'message' => 'Could not retrieve data'], 500);
}
}
When I run this when a record exists I receive the following which is what I expect.
{
"status": "ok",
"data": {
"id": 98,
"market_id": 1,
"crop_id": 2,
"chemical_id": 113,
"maximum_residue_level": null,
"exempt": 0,
"comments": null,
"date_verified": "2021-10-07",
"created_at": "2021-10-19T05:42:12.000000Z",
"updated_at": "2021-10-19T05:42:12.000000Z"
}
}
However, when I enter an ID in the route endpoint for a record that does not exist, I receive the following:
{
"message": "No query results for model [App\\Seasonal\\Mrl] 99"
}
This is happening from what I understand to be the auto find of the record between the Route and the controller, and I am lost as to how to customize this behavior.
Thanks in advance for your help!
you don't show the code where you are fetchning the model from the database but we can assume something like that:
$mrl = Mrl::findOrFail($id);
show($mrl);
The model findOrFail() method throws an exception when the model is not found, which is convenient when you want to adapt the response.
You can imagine something like that:
try {
$mrl = Mrl::findOrFail($id);
return response(['status' => 'ok', 'data' => $data], 200);
} catch (ModelNotFoundException $e) {
return response(['status' => 'error', 'message' => 'Could not retrieve data'], 500);
}
The idea is to catch the error thrown by your model to change the message and status code of the response.
When building APIs you should event add a "generic" catch statement for any unhandled errors to display a standardized generic error message and log what happened, like this:
try {
$mrl = Mrl::findOrFail($id);
// Do more things that could generate errors ?
return response(['status' => 'ok', 'data' => $data], 200);
} catch (ModelNotFoundException $e) {
// Not found
return response(['status' => 'error', 'message' => 'Could not retrieve data'], 500);
} catch (\Exception $e) {
// Generic error
\Log::error($e);
return response(['status' => 'error', 'message' => 'An error occured'], 500);
}
After spending time reading the docs I finally found the answer on how to customize the behaviour if a record is found when utilising Eloquents Route Model Binding.
In the Laravel docs, there is a method for this explicit purpose, to override the default behaviour when the bound model does not have a valid record.
The method for doing this is to chain the ->missing() function call to the end of your route and then providing the behaviour for the response you wish to provide. As below:
Route::get('{mrl}', [MrlController::class, 'show'])->missing(function () {
return response(['status' => 'error', 'message' => 'Invalid query.'], 404);
});
Chaining this method onto my request has enabled me to return the generic response I was hoping for when querying an invalid model record.

Axios returns error status code 500 when there is data present

I am using Laravel 8, VueJS and Axios for my application then every time I try to fetch all records from my database it returns an error with status code 500. Even though when fetching the data using Postman/Insomnia it returns the data without an error.
I tried to empty the table where it fetches the data the error disappears and it returns empty data with status code 200.
Store Module:
import axios from 'axios'
export default {
namespaced: true,
state: {
courses: [],
teacher: '',
},
getters: {
allCourses(state) {
return state.courses
},
},
actions: {
async fetchAllCourses({ commit }) {
const response = await axios.get('teacher/course-management/list')
console.log(response.data.data)
commit('SET_COURSES', response.data.data)
}
},
mutations: {
SET_COURSES(state, courses) {
state.courses = courses
}
}
Controller:
public function fetchAllCourses() {
try {
$courses = Course::all()->sortBy('id');
$data = $courses->transform(function ($course) {
// ! Get teacher id
$teacherId = $this->user->teacher->id;
// ! Get teacher name by id
$teacherName = $this->getTeacherName($teacherId);
return [
'id' => $course->id,
'teacher_id' => $course->teacher_id,
'teacher' => $teacherName,
'section' => $course->section,
'code' => $course->code,
'status' => $course->status,
'image' => $course->image,
];
});
return $this->success('Request success', $data);
} catch (\Exception $e) {
return $this->error($e->getMessage(), $e->getCode());
}
}
Problem solved.
public function fetchAllCourses() {
try {
$courses = Course::all()->sortBy('id');
$data = $courses->transform(function ($course) {
return [
'id' => $course->id,
'teacher_id' => $course->teacher_id,
'teacher' => $this->getTeacherName($course->teacher_id),
'section' => $course->section,
'code' => $course->code,
'status' => $course->status,
'image' => $course->image,
];
});
return $this->success('Request success', $data);
} catch (\Exception $e) {
return $this->error($e->getMessage(), $e->getCode());
}
}

Problems with request when I change a value by parameter in Laravel

I have a function that asks for a value per parameter from a table.When the request is made there are some values ​​that are in the table that is not reading them, while others are.All of them are in the table. So it shouldn't give null.
function:
public function showMarketByGroup($idGroup)
{
$response = array('code' => 400, 'error_msg' => []);
try {
$karateka = Karateka::find($idGroup);
if (!empty($karateka)) {
$karatekasInMarket = $karateka->karatekasByGroupInMarket;
return $response = array('code' => 200, 'karatekas' => $karatekasInMarket, 'msg' => 'Get all karatekas by group in market');
} else {
return $response = array('code' => 401, 'error_msg' => 'Unautorized');
}
} catch (\Exception $exception) {
$response = array('code' => 500, 'error_msg' => $exception->getMessage());
}
return response($response, $response['code']);
}
This is the table where I am referencing:
When the column of the id_group has a value of 3 or 4, the answer is correct.But with the other values ​​it enters an exception in Postman:
{
"code": 401,
"error_msg": "Unautorized"
}

Laravel 5.5 - Handle Error Exception for 'where'

In Laravel 5.5 I am trying to handle an error exception like this...
try {
$fruits = Fruit::where('fruit_id', $user->fruit->id)->get();
}
catch(ModelNotFoundException $e) {
return Response::json(array(
'error' => true,
'status_code' => 400,
'response' => 'fruit_id not found',
));
}
But this is giving me a 'Trying to get propert of non-object' error
The same error handling works correctly for findorfail, how should I be doing this for the 'where' statement?
I think you are passing wrong values in your where query in the try block.
try {
$fruits = Fruit::where('fruit_id', $user->fruit->id)->get();
}
Is it fruit_id or just id because you are querying it on fruit model itself.
Thanks to some pointers in the comments I changed to
try {
$fruits = Fruit::where('fruit_id', $user->fruit->id)->get();
}
catch(\Exception $e) {
return Response::json(array(
'error' => true,
'status_code' => 400,
'response' => 'fruit_id not found',
));
}
All is now working now I am catching the correct exception
Tested on Laravel 5.7
try {
$fruits = Fruit::where('fruit_id', $user->fruit->id)->get();
}
catch(\Exception $e) {
abort(404);
}
Even though you got it working, I'd like to make mention of Laravel's Exception Handler.
The report method allows you to catch any exception type and customize how you wish to process and move forward. There is a report helper function as well which is globally accessible.
Furthermore, reportable and renderable exceptions allow you to create and customize your responses.
Try the below code:
if ($user->fruit) {
try {
$fruits = Fruit::where('fruit_id', $user->fruit->id)->get();
} catch(\Exception $e) {
return Response::json(array(
'error' => true,
'status_code' => 400,
'response' => 'fruit_id not found',
));
}
} else {
return "User Fruit doesn't exists!!";
}

Laravel 5.3 DB:transaction is committing even some queries failed

I have a User model for basic user info, then a Role model and RoleUser model to associate roles with user. On user edit form, additional role can be added to that user. So, here two DB operations are done within a DB::transaction,
1) Update User info into User model
2) Add role to user
The issue is, if "Add role to user" fails, it doesn't Rollback changes in "User" model which already updated successfully.
Here is my sample code-
In Controller:
$response =
DB::transaction(function() use($user_data, $input) {
//Update changes in basic user info using "User" Model
$response = User::updateUser($user_data['user']);
if ($response->status===FALSE) {//not updated
return $response;
}
if (!empty($user_data['roles'])) {
$roles = [];
foreach ($user_data['roles'] as $role) {
$roles[] = ['role_id' => $role, 'user_id' => $user_data['user']['id'], 'created_by' => $this->curr_user->id, 'updated_by' => $this->curr_user->id];
}
//Add new roles to the user using "RoleUser" Model
$response3 = RoleUser::createRoleUser($roles);
if ($response3->status===FALSE) {//failed to add
return $response3;
}
}
return $response;
}, 5);
//source of createRoleUser method in RoleUser model
try {
DB::table($table)->where('id', $id)->update($changes);
} catch (\Illuminate\Database\QueryException $qe) {
return (object) ['status' => FALSE, 'error' => $qe->errorInfo];
} catch (\Exception $e) {
return (object) ['status' => FALSE, 'error' => [$e->getCode(), 'non-DB', $e->getMessage()]];
}
return (object) ['status' => TRUE, 'data' => $changes + ['id' => $id]];
//source of createRoleUser method in RoleUser model
try {
$new_rec_id = DB::table('role_users)->insertGetId($new_data);
$new_rec = FALSE;
if ($new_rec_id) {
$new_rec = DB::table($table)->where('id', $new_rec_id)->first();
}
} catch (\Illuminate\Database\QueryException $qe) {
return (object) ['status' => FALSE, 'error' => $qe->errorInfo];
} catch (\Exception $e) {
return (object) ['status' => FALSE, 'error' => [$e->getCode(), 'non-DB', $e->getMessage()]];
}
return (object) ['status' => TRUE, 'data' => $new_rec];
You have to throw an exception from within the transaction closure in order for the transaction to trigger the rollback. If no exception is thrown, the transaction will commit.
Keeping this in mind, that means the call to the transaction function needs to be wrapped in a try/catch, as the code that handles the rollback will rethrow the exception after the rollback for your application code to handle.
So, your code would look something like:
try {
$response = DB::transaction(function() use($user_data, $input) {
//Update changes in basic user info using "User" Model
$response = User::updateUser($user_data['user']);
if ($response->status===FALSE) {//not updated
// throw exception to trigger rollback
throw new \Exception($response->error);
}
if (!empty($user_data['roles'])) {
$roles = [];
foreach ($user_data['roles'] as $role) {
$roles[] = ['role_id' => $role, 'user_id' => $user_data['user']['id'], 'created_by' => $this->curr_user->id, 'updated_by' => $this->curr_user->id];
}
//Add new roles to the user using "RoleUser" Model
$response3 = RoleUser::createRoleUser($roles);
if ($response3->status===FALSE) {//failed to add
// throw exception to trigger rollback
throw new \Exception($response3->error);
}
}
// return without exception to trigger commit
return $response;
}, 5);
} catch (\Exception $e) {
echo 'uh oh: '.$e->getMessage();
}

Resources