From 956fb95a482aa357bcc67b7ad20937ad11a51657 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Tue, 14 Jan 2020 12:23:31 +0100 Subject: [PATCH] Better errors handling for TwoFAccount controller --- app/Http/Controllers/IconController.php | 2 +- app/Http/Controllers/QrCodeController.php | 4 +- .../Controllers/TwoFAccountController.php | 10 ++--- resources/js/views/Create.vue | 37 ++++++++++++------- resources/js/views/Edit.vue | 33 +++++++++++------ 5 files changed, 52 insertions(+), 34 deletions(-) diff --git a/app/Http/Controllers/IconController.php b/app/Http/Controllers/IconController.php index 9bdd1a1f..51b1ecba 100644 --- a/app/Http/Controllers/IconController.php +++ b/app/Http/Controllers/IconController.php @@ -24,7 +24,7 @@ public function upload(Request $request) ]); if ($validator->fails()) { - return response()->json(['error' => $validator->errors()], 400); + return response()->json(['validation' => $validator->errors()], 400); } $path = $request->file('icon')->storePublicly('public/icons'); diff --git a/app/Http/Controllers/QrCodeController.php b/app/Http/Controllers/QrCodeController.php index 99683594..fb5ce702 100644 --- a/app/Http/Controllers/QrCodeController.php +++ b/app/Http/Controllers/QrCodeController.php @@ -28,7 +28,7 @@ public function decode(Request $request) ]); if ($validator->fails()) { - return response()->json(['error' => $validator->errors()], 400); + return response()->json(['validation' => $validator->errors()], 400); } // qrcode analysis @@ -65,7 +65,7 @@ public function decode(Request $request) catch (AssertionFailedException $exception) { return response()->json([ - 'error' => [ + 'validation' => [ 'qrcode' => __('errors.response.no_valid_totp') ] ], 400); diff --git a/app/Http/Controllers/TwoFAccountController.php b/app/Http/Controllers/TwoFAccountController.php index 2331b76a..c37e5e01 100644 --- a/app/Http/Controllers/TwoFAccountController.php +++ b/app/Http/Controllers/TwoFAccountController.php @@ -40,7 +40,7 @@ public function store(Request $request) ]); if ($validator->fails()) { - return response()->json(['error' => $validator->errors()], 400); + return response()->json(['validation' => $validator->errors()], 400); } $twofaccount = TwoFAccount::create([ @@ -66,7 +66,7 @@ public function show($id) $twofaccount = TwoFAccount::FindOrFail($id); return response()->json($twofaccount, 200); } catch (\Exception $e) { - return response()->json( ['error' => 'not found' ], 404); + return response()->json( ['message' => 'not found' ], 404); } } @@ -100,7 +100,7 @@ public function update(Request $request, $id) ]); if ($validator->fails()) { - return response()->json( ['error' => $validator->errors() ], 400); + return response()->json( ['validation' => $validator->errors() ], 400); } @@ -114,7 +114,7 @@ public function update(Request $request, $id) } catch (\Exception $e) { - return response()->json( ['error' => 'not found' ] , 404); + return response()->json( ['message' => 'not found' ] , 404); } } @@ -146,7 +146,7 @@ public function destroy($id) } catch (\Exception $e) { - return response()->json('already gone', 404); + return response()->json(['message' => 'already gone'], 404); } } diff --git a/resources/js/views/Create.vue b/resources/js/views/Create.vue index f50a6d39..60f22987 100644 --- a/resources/js/views/Create.vue +++ b/resources/js/views/Create.vue @@ -17,20 +17,20 @@ -

{{ errors.qrcode.toString() }}

+

{{ validationErrors.qrcode.toString() }}

-

{{ errors.service.toString() }}

+

{{ validationErrors.service.toString() }}

-

{{ errors.account.toString() }}

+

{{ validationErrors.account.toString() }}

@@ -54,7 +54,7 @@
-

{{ errors.uri.toString() }}

+

{{ validationErrors.uri.toString() }}

@@ -73,7 +73,7 @@
-

{{ errors.icon.toString() }}

+

{{ validationErrors.icon.toString() }}

@@ -100,7 +100,7 @@ }, uriIsLocked: true, tempIcon: '', - errors: {} + validationErrors: {} } }, @@ -121,8 +121,11 @@ this.$router.push({name: 'accounts', params: { InitialEditMode: false }}); }) .catch(error => { - if (error.response.status === 400) { - this.errors = error.response.data.error + if( error.response.data.validation ) { + this.validationErrors = error.response.data.validation + } + else { + this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); } }); }, @@ -156,13 +159,16 @@ axios.post('/api/qrcode/decode', imgdata, config) .then(response => { this.twofaccount = response.data; - this.errors['qrcode'] = ''; + this.validationErrors['qrcode'] = ''; }) .catch(error => { - if (error.response.status === 400) { - this.errors = error.response.data.error + if( error.response.data.validation ) { + this.validationErrors = error.response.data.validation this.clearTwofaccount() } + else { + this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); + } }); }, @@ -192,11 +198,14 @@ .then(response => { console.log('icon path > ', response); this.tempIcon = response.data; - this.errors['icon'] = ''; + this.validationErrors['icon'] = ''; }) .catch(error => { - if (error.response.status === 400) { - this.errors = error.response.data.error + if( error.response.data.validation ) { + this.validationErrors = error.response.data.validation + } + else { + this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); } }); diff --git a/resources/js/views/Edit.vue b/resources/js/views/Edit.vue index 672908be..3cb8ae47 100644 --- a/resources/js/views/Edit.vue +++ b/resources/js/views/Edit.vue @@ -9,14 +9,14 @@
-

{{ errors.service.toString() }}

+

{{ validationErrors.service.toString() }}

-

{{ errors.account.toString() }}

+

{{ validationErrors.account.toString() }}

@@ -36,7 +36,7 @@
-

{{ errors.icon.toString() }}

+

{{ validationErrors.icon.toString() }}

@@ -63,7 +63,7 @@ }, twofaccountExists: false, tempIcon: '', - errors: {} + validationErrors: {} } }, @@ -86,9 +86,12 @@ this.tempIcon = this.twofaccount.icon }) .catch(error => { - if (error.response.status === 404) { + if( error.response.status === 404 ) { this.$router.push({ name: '404' }); } + else { + this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); + } }); }, @@ -116,12 +119,15 @@ this.$router.push({name: 'accounts', params: { InitialEditMode: true }}); }) .catch(error => { - if (error.response.status === 400) { - this.errors = error.response.data.error - } - else if (error.response.status === 404) { + if (error.response.status === 404) { this.$router.push({ name: '404' }); } + else if( error.response.data.validation ) { + this.validationErrors = error.response.data.validation + } + else { + this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); + } }); }, @@ -160,11 +166,14 @@ .then(response => { console.log('icon path > ', response); this.tempIcon = response.data; - this.errors['icon'] = ''; + this.validationErrors['icon'] = ''; }) .catch(error => { - if (error.response.status === 400) { - this.errors = error.response.data.error + if( error.response.data.validation ) { + this.validationErrors = error.response.data.validation + } + else { + this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); } }); },