Skip to content

Commit 2266253

Browse files
authored
update: improve error handling for developers
1 parent 17bd16c commit 2266253

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

src/Flutterwave.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class Flutterwave
2727

2828
private \Psr\Log\LoggerInterface $logger;
2929

30-
const VERSION = "1.0.4";
30+
const VERSION = "2.1.1";
3131

3232
/**
3333
* Flutterwave constructor
@@ -116,4 +116,4 @@ private function loadConfig(): void
116116
];
117117
// TODO: $this->config = config('flutterwave');
118118
}
119-
}
119+
}

src/Http/ConfirmRequest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,21 @@ public function messages(): array
5757
*/
5858
public function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
5959
{
60+
$logger = Log::channel('flutterwave');
61+
$logger->error('Flutterwave Validation failed in ConfirmRequest:', $validator->errors()->toArray());
62+
// parent::failedValidation($validator); // Let Laravel handle the rest of the failed validation.
63+
$errors = (new ValidationException($validator))->errors();
64+
6065
if (!app()->isProduction()) {
61-
Log::error('Flutterwave Validation failed in ConfirmRequest:', $validator->errors()->toArray());
66+
throw new InvalidArgument("Flutterwave Validation failed in ConfirmRequest", $errors);
6267
}
6368

64-
parent::failedValidation($validator); // Let Laravel handle the rest of the failed validation
69+
throw new HttpResponseException(response()->json(
70+
[
71+
'error' => $errors,
72+
'status_code' => JsonResponse::HTTP_UNPROCESSABLE_ENTITY,
73+
],
74+
JsonResponse::HTTP_UNPROCESSABLE_ENTITY
75+
));
6576
}
6677
}

src/Http/PaymentRequest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
use Illuminate\Foundation\Http\FormRequest;
88
use Illuminate\Support\Facades\Log;
9+
use Illuminate\Validation\ValidationException;
10+
use Illuminate\Http\Exceptions\HttpResponseException;
11+
use Illuminate\Http\Response;
12+
use Illuminate\Http\JsonResponse;
13+
use Flutterwave\Payments\Exception\InvalidArgument;
914

1015
class PaymentRequest extends FormRequest
1116
{
@@ -57,9 +62,20 @@ public function messages(): array
5762
*/
5863
public function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
5964
{
65+
$logger = Log::channel('flutterwave');
66+
$logger->error('Flutterwave Validation failed in PaymentRequest:', $validator->errors()->toArray());
67+
$errors = (new ValidationException($validator))->errors();
68+
6069
if (!app()->isProduction()) {
61-
Log::error('Flutterwave Validation failed in ConfirmRequest:', $validator->errors()->toArray());
70+
throw new InvalidArgument("Flutterwave Validation failed in PaymentRequest", $errors);
6271
}
63-
parent::failedValidation($validator);
72+
73+
throw new HttpResponseException(response()->json(
74+
[
75+
'error' => $errors,
76+
'status_code' => JsonResponse::HTTP_UNPROCESSABLE_ENTITY,
77+
],
78+
JsonResponse::HTTP_UNPROCESSABLE_ENTITY
79+
));
6480
}
6581
}

src/Services/Transactions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(Api $api, array $config)
4242
}
4343

4444
private function handleMissingSecretKey( $config ): void {
45-
if( !isset( $config['secret_key'] )) {
45+
if( !isset( $config['secret_key'] ) || empty( $config['secret_key'] ) ) {
4646
throw new InvalidArgument('The secret key is required. please add it to the .env file.');
4747
}
4848
}

src/resources/views/errors/invalid.blade.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@
184184
<h1>Error: Something Went Wrong.</h1>
185185
<p>Oops, something went wrong while processing the payment with Flutterwave. Please check the error details below to help you debug.</p>
186186

187+
@if(isset($_GET['errors']) && is_array($_GET['errors']))
188+
<div class="error-details">
189+
<strong>Validation Errors:</strong>
190+
<ul>
191+
@foreach($_GET['errors'] as $field => $messages)
192+
@foreach((array)$messages as $message)
193+
<li><strong>{{ $field }}:</strong> {{ $message }}</li>
194+
@endforeach
195+
@endforeach
196+
</ul>
197+
</div>
198+
@else
187199
<div id="stackTraceText" class="error-details">
188200
<strong>Error Message:</strong>
189201
<p>{{ $_GET['message'] ?? 'No error message available.' }}</p>
@@ -198,7 +210,7 @@
198210
<pre>{{ $stackTrace ?? 'No stack trace available.' }}</pre>
199211
<button class="copy-btn copy-btn-stacktrace" onclick="copyText('stackTraceText')">Copy Stack Trace</button>
200212
</div>
201-
213+
@endif
202214
<!-- System Information Section -->
203215
<div id="systemInfoText" class="system-info">
204216
<h2>System Information</h2>

0 commit comments

Comments
 (0)