Best Practices for Error Handling in Production Server Code (example code)
When developing applications for production servers, it's crucial to implement robust error handling mechanisms to ensure smooth operation and prevent unexpected failures.
Here's an example of how to handle errors effectively in production server code in Laravel:
Create a wallet column in users table using migration command
php artisan make:migration add_wallet_column_to_users_table --table=users
Add code to migration file database\migrations\2024_03_18_053755_add_wallet_column_to_users_table.php
and run php artisan migrate
command to run migration after copy/paste code in it.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->integer('wallet')->unsigned()->default(2);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('wallet');
});
}
};
Add below code to web.php file
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
Route::get('/buy/{cookie}', function($cookie) {
$validator = Validator::make(['cookie' => $cookie], [
'cookie' => 'required|integer|min:1'
]);
if($validator->fails()){
return 'Invalid number of cookies!';
}
if (Auth::check()) {
$wallet = Auth::user()->wallet;
$purchaseAmount = $cookie * 1;
if ($wallet < $purchaseAmount) {
return 'Insufficient balance';
}
DB::beginTransaction();
try{
User::where(['id' => Auth::user()->id])
->update(['wallet' => ($wallet - $purchaseAmount)]);
DB::commit();
return 'Purchase successful!';
}catch(Exception $e){
DB::rollBack();
throw new UnprocessableEntityHttpException($e->getMessage());
}
} else {
return 'You must be logged in to make a purchase!';
}
});
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Validation for multiple forms on same page in laravel
- Laravel 10 starter app using breeze on live server
- Rename Pivot Table in Laravel
- Laravel 10 Breeze Authentication Example
- How to decrypt laravel password
- Class "App\Http\Controllers\Auth\Verified" not found
- How to display HTML tags In Laravel blade
- There are no commands defined in the "route:" namespace
- How to get tomorrow and yesterday date in laravel
- Class 'App\Rules\Hash' not found in Laravel
- Extra Filter Query on Relationships in Laravel
- How to avoid duplicate entries in pivot table in Laravel
- Page loader in laravel
- Class 'App\Providers\Auth' not found
- How to get database name in Laravel 9 ?
- How to get session in blade Laravel ?
- Laravel specific table Migration
- Call to undefined method Illuminate\Support\Facades\Request::all()
- Global scope in Laravel with example
- Order by multiple columns in Laravel
- How to validate form input data in laravel
- Get posts belongs to a specific user in Laravel
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- Wheredate in laravel not working
- Laravel create table migration with model