浏览代码

Better errors handling for TwoFAccount controller

Bubka 5 年之前
父节点
当前提交
956fb95a48

+ 1 - 1
app/Http/Controllers/IconController.php

@@ -24,7 +24,7 @@ class IconController extends Controller
         ]);
 
         if ($validator->fails()) {
-            return response()->json(['error' => $validator->errors()], 400);
+            return response()->json(['validation' => $validator->errors()], 400);
         }
         
         $path = $request->file('icon')->storePublicly('public/icons');

+ 2 - 2
app/Http/Controllers/QrCodeController.php

@@ -28,7 +28,7 @@ class QrCodecontroller extends Controller
         ]);
 
         if ($validator->fails()) {
-            return response()->json(['error' => $validator->errors()], 400);
+            return response()->json(['validation' => $validator->errors()], 400);
         }
 
         // qrcode analysis
@@ -65,7 +65,7 @@ class QrCodecontroller extends Controller
         catch (AssertionFailedException $exception) {
 
             return response()->json([
-                'error' => [
+                'validation' => [
                    'qrcode' => __('errors.response.no_valid_totp')
                 ]
             ], 400);

+ 5 - 5
app/Http/Controllers/TwoFAccountController.php

@@ -40,7 +40,7 @@ class TwoFAccountController extends Controller
         ]);
 
         if ($validator->fails()) {
-            return response()->json(['error' => $validator->errors()], 400);
+            return response()->json(['validation' => $validator->errors()], 400);
         }
 
         $twofaccount = TwoFAccount::create([
@@ -66,7 +66,7 @@ class TwoFAccountController extends Controller
             $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 @@ class TwoFAccountController extends Controller
         ]);
 
         if ($validator->fails()) {
-            return response()->json( ['error' => $validator->errors() ], 400);
+            return response()->json( ['validation' => $validator->errors() ], 400);
         }
 
 
@@ -114,7 +114,7 @@ class TwoFAccountController extends Controller
         }
         catch (\Exception $e) {
 
-            return response()->json( ['error' => 'not found' ] , 404);
+            return response()->json( ['message' => 'not found' ] , 404);
 
         }
     }
@@ -146,7 +146,7 @@ class TwoFAccountController extends Controller
         }
         catch (\Exception $e) {
 
-            return response()->json('already gone', 404);
+            return response()->json(['message' => 'already gone'], 404);
 
         }
     }

+ 23 - 14
resources/js/views/Create.vue

@@ -17,20 +17,20 @@
                             </label>
                         </div>
                     </div>
-                    <p class="help is-danger help-for-file" v-if="errors.qrcode">{{ errors.qrcode.toString() }}</p>
+                    <p class="help is-danger help-for-file" v-if="validationErrors.qrcode">{{ validationErrors.qrcode.toString() }}</p>
                     <div class="field">
                         <label class="label">{{ $t('twofaccounts.service') }}</label>
                         <div class="control">
                             <input class="input" type="text" :placeholder="$t('twofaccounts.forms.service.placeholder')" v-model="twofaccount.service"  autofocus />
                         </div>
-                        <p class="help is-danger" v-if="errors.service">{{ errors.service.toString() }}</p>
+                        <p class="help is-danger" v-if="validationErrors.service">{{ validationErrors.service.toString() }}</p>
                     </div>
                     <div class="field">
                         <label class="label">{{ $t('twofaccounts.account') }}</label>
                         <div class="control">
                             <input class="input" type="text" :placeholder="$t('twofaccounts.forms.account.placeholder')" v-model="twofaccount.account"  />
                         </div>
-                        <p class="help is-danger" v-if="errors.account">{{ errors.account.toString() }}</p>
+                        <p class="help is-danger" v-if="validationErrors.account">{{ validationErrors.account.toString() }}</p>
                     </div>
                     <div class="field" style="margin-bottom: 0.5rem;">
                         <label class="label">{{ $t('twofaccounts.forms.totp_uri') }}</label>
@@ -54,7 +54,7 @@
                             </a>
                         </div>
                     </div>
-                    <p class="help is-danger help-for-file" v-if="errors.uri">{{ errors.uri.toString() }}</p>
+                    <p class="help is-danger help-for-file" v-if="validationErrors.uri">{{ validationErrors.uri.toString() }}</p>
                     <div class="field">
                         <label class="label">{{ $t('twofaccounts.icon') }}</label>
                         <div class="file is-dark">
@@ -73,7 +73,7 @@
                             </span>
                         </div>
                     </div>
-                    <p class="help is-danger help-for-file" v-if="errors.icon">{{ errors.icon.toString() }}</p>
+                    <p class="help is-danger help-for-file" v-if="validationErrors.icon">{{ validationErrors.icon.toString() }}</p>
                     <div class="field is-grouped">
                         <div class="control">
                             <button type="submit" class="button is-link">{{ $t('twofaccounts.forms.create') }}</button>
@@ -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 } });
                         }
                     });
 

+ 21 - 12
resources/js/views/Edit.vue

@@ -9,14 +9,14 @@
                         <div class="control">
                             <input class="input" type="text" :placeholder="$t('twofaccounts.forms.service.placeholder')" v-model="twofaccount.service" autofocus />
                         </div>
-                        <p class="help is-danger" v-if="errors.service">{{ errors.service.toString() }}</p>
+                        <p class="help is-danger" v-if="validationErrors.service">{{ validationErrors.service.toString() }}</p>
                     </div>
                     <div class="field">
                         <label class="label">{{ $t('twofaccounts.account') }}</label>
                         <div class="control">
                             <input class="input" type="text" :placeholder="$t('twofaccounts.forms.account.placeholder')" v-model="twofaccount.account" />
                         </div>
-                        <p class="help is-danger" v-if="errors.account">{{ errors.account.toString() }}</p>
+                        <p class="help is-danger" v-if="validationErrors.account">{{ validationErrors.account.toString() }}</p>
                     </div>
                     <div class="field">
                         <label class="label">{{ $t('twofaccounts.icon') }}</label>
@@ -36,7 +36,7 @@
                             </span>
                         </div>
                     </div>
-                    <p class="help is-danger help-for-file" v-if="errors.icon">{{ errors.icon.toString() }}</p>
+                    <p class="help is-danger help-for-file" v-if="validationErrors.icon">{{ validationErrors.icon.toString() }}</p>
                     <div class="field is-grouped">
                         <div class="control">
                             <button type="submit" class="button is-link">{{ $t('twofaccounts.forms.save') }}</button>
@@ -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 } });
                         }
                     });
             },