Skip to content

Commit 91fa8d4

Browse files
Show server error in modal
1 parent 08cc12d commit 91fa8d4

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

app/Exceptions/Handler.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Exceptions;
44

5+
use Throwable;
56
use Illuminate\Auth\AuthenticationException;
67
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
78

@@ -39,4 +40,22 @@ protected function unauthenticated($request, AuthenticationException $exception)
3940
? response()->json(['message' => $exception->getMessage()], 401)
4041
: redirect()->guest(url('/login'));
4142
}
43+
44+
/**
45+
* Prepare a JSON response for the given exception.
46+
*
47+
* @param \Illuminate\Http\Request $request
48+
* @param \Throwable $e
49+
* @return \Symfony\Component\HttpFoundation\Response
50+
*/
51+
protected function prepareJsonResponse($request, Throwable $e)
52+
{
53+
$response = parent::prepareJsonResponse($request, $e);
54+
55+
if ($response->getStatusCode() === 500 && config('app.debug')) {
56+
return $this->prepareResponse($request, $e);
57+
}
58+
59+
return $response;
60+
}
4261
}

resources/js/plugins/axios.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ axios.interceptors.request.use(request => {
2525
axios.interceptors.response.use(response => response, error => {
2626
const { status } = error.response
2727

28-
if (status >= 500) {
29-
Swal.fire({
30-
icon: 'error',
31-
title: i18n.t('error_alert_title'),
32-
text: i18n.t('error_alert_text'),
33-
reverseButtons: true,
34-
confirmButtonText: i18n.t('ok'),
35-
cancelButtonText: i18n.t('cancel')
36-
})
37-
}
38-
3928
if (status === 401 && store.getters['auth/check']) {
4029
Swal.fire({
4130
icon: 'warning',
@@ -51,5 +40,44 @@ axios.interceptors.response.use(response => response, error => {
5140
})
5241
}
5342

43+
if (status >= 500) {
44+
serverError(error.response)
45+
}
46+
5447
return Promise.reject(error)
5548
})
49+
50+
let serverErrorModalShown = false
51+
async function serverError (response) {
52+
if (serverErrorModalShown) {
53+
return
54+
}
55+
56+
if ((response.headers['content-type'] || '').includes('text/html')) {
57+
const iframe = document.createElement('iframe')
58+
59+
if (response.data instanceof Blob) {
60+
iframe.srcdoc = await response.data.text()
61+
} else {
62+
iframe.srcdoc = response.data
63+
}
64+
65+
Swal.fire({
66+
html: iframe.outerHTML,
67+
showConfirmButton: false,
68+
customClass: { container: 'server-error-modal' },
69+
didDestroy: () => { serverErrorModalShown = false }
70+
})
71+
72+
serverErrorModalShown = true
73+
} else {
74+
Swal.fire({
75+
icon: 'error',
76+
title: i18n.t('error_alert_title'),
77+
text: i18n.t('error_alert_text'),
78+
reverseButtons: true,
79+
confirmButtonText: i18n.t('ok'),
80+
cancelButtonText: i18n.t('cancel')
81+
})
82+
}
83+
}

resources/sass/app.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
@import 'elements/navbar';
77
@import 'elements/buttons';
88
@import 'elements/transitions';
9+
10+
@import 'components/server-error';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.server-error-modal {
2+
height: 100%;
3+
4+
.swal2-modal {
5+
padding: 0;
6+
width: calc(100% - 60px);
7+
height: calc(100% - 60px);
8+
}
9+
10+
.swal2-content,
11+
.swal2-html-container {
12+
padding: 0;
13+
height: 100%;
14+
}
15+
16+
iframe {
17+
width: 100%;
18+
height: 100%;
19+
border: none;
20+
border-radius: 5px;
21+
}
22+
}

0 commit comments

Comments
 (0)