Better errors handling for TwoFAccount controller

This commit is contained in:
Bubka 2020-01-14 12:23:31 +01:00
parent 99a2fc8641
commit 956fb95a48
5 changed files with 52 additions and 34 deletions

View File

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

View File

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

View File

@ -40,7 +40,7 @@ public function store(Request $request)
]); ]);
if ($validator->fails()) { if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 400); return response()->json(['validation' => $validator->errors()], 400);
} }
$twofaccount = TwoFAccount::create([ $twofaccount = TwoFAccount::create([
@ -66,7 +66,7 @@ public function show($id)
$twofaccount = TwoFAccount::FindOrFail($id); $twofaccount = TwoFAccount::FindOrFail($id);
return response()->json($twofaccount, 200); return response()->json($twofaccount, 200);
} catch (\Exception $e) { } 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()) { 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) { 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) { catch (\Exception $e) {
return response()->json('already gone', 404); return response()->json(['message' => 'already gone'], 404);
} }
} }

View File

@ -17,20 +17,20 @@
</label> </label>
</div> </div>
</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"> <div class="field">
<label class="label">{{ $t('twofaccounts.service') }}</label> <label class="label">{{ $t('twofaccounts.service') }}</label>
<div class="control"> <div class="control">
<input class="input" type="text" :placeholder="$t('twofaccounts.forms.service.placeholder')" v-model="twofaccount.service" autofocus /> <input class="input" type="text" :placeholder="$t('twofaccounts.forms.service.placeholder')" v-model="twofaccount.service" autofocus />
</div> </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>
<div class="field"> <div class="field">
<label class="label">{{ $t('twofaccounts.account') }}</label> <label class="label">{{ $t('twofaccounts.account') }}</label>
<div class="control"> <div class="control">
<input class="input" type="text" :placeholder="$t('twofaccounts.forms.account.placeholder')" v-model="twofaccount.account" /> <input class="input" type="text" :placeholder="$t('twofaccounts.forms.account.placeholder')" v-model="twofaccount.account" />
</div> </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>
<div class="field" style="margin-bottom: 0.5rem;"> <div class="field" style="margin-bottom: 0.5rem;">
<label class="label">{{ $t('twofaccounts.forms.totp_uri') }}</label> <label class="label">{{ $t('twofaccounts.forms.totp_uri') }}</label>
@ -54,7 +54,7 @@
</a> </a>
</div> </div>
</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"> <div class="field">
<label class="label">{{ $t('twofaccounts.icon') }}</label> <label class="label">{{ $t('twofaccounts.icon') }}</label>
<div class="file is-dark"> <div class="file is-dark">
@ -73,7 +73,7 @@
</span> </span>
</div> </div>
</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="field is-grouped">
<div class="control"> <div class="control">
<button type="submit" class="button is-link">{{ $t('twofaccounts.forms.create') }}</button> <button type="submit" class="button is-link">{{ $t('twofaccounts.forms.create') }}</button>
@ -100,7 +100,7 @@
}, },
uriIsLocked: true, uriIsLocked: true,
tempIcon: '', tempIcon: '',
errors: {} validationErrors: {}
} }
}, },
@ -121,8 +121,11 @@
this.$router.push({name: 'accounts', params: { InitialEditMode: false }}); this.$router.push({name: 'accounts', params: { InitialEditMode: false }});
}) })
.catch(error => { .catch(error => {
if (error.response.status === 400) { if( error.response.data.validation ) {
this.errors = error.response.data.error 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) axios.post('/api/qrcode/decode', imgdata, config)
.then(response => { .then(response => {
this.twofaccount = response.data; this.twofaccount = response.data;
this.errors['qrcode'] = ''; this.validationErrors['qrcode'] = '';
}) })
.catch(error => { .catch(error => {
if (error.response.status === 400) { if( error.response.data.validation ) {
this.errors = error.response.data.error this.validationErrors = error.response.data.validation
this.clearTwofaccount() this.clearTwofaccount()
} }
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
}); });
}, },
@ -192,11 +198,14 @@
.then(response => { .then(response => {
console.log('icon path > ', response); console.log('icon path > ', response);
this.tempIcon = response.data; this.tempIcon = response.data;
this.errors['icon'] = ''; this.validationErrors['icon'] = '';
}) })
.catch(error => { .catch(error => {
if (error.response.status === 400) { if( error.response.data.validation ) {
this.errors = error.response.data.error this.validationErrors = error.response.data.validation
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
} }
}); });

View File

@ -9,14 +9,14 @@
<div class="control"> <div class="control">
<input class="input" type="text" :placeholder="$t('twofaccounts.forms.service.placeholder')" v-model="twofaccount.service" autofocus /> <input class="input" type="text" :placeholder="$t('twofaccounts.forms.service.placeholder')" v-model="twofaccount.service" autofocus />
</div> </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>
<div class="field"> <div class="field">
<label class="label">{{ $t('twofaccounts.account') }}</label> <label class="label">{{ $t('twofaccounts.account') }}</label>
<div class="control"> <div class="control">
<input class="input" type="text" :placeholder="$t('twofaccounts.forms.account.placeholder')" v-model="twofaccount.account" /> <input class="input" type="text" :placeholder="$t('twofaccounts.forms.account.placeholder')" v-model="twofaccount.account" />
</div> </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>
<div class="field"> <div class="field">
<label class="label">{{ $t('twofaccounts.icon') }}</label> <label class="label">{{ $t('twofaccounts.icon') }}</label>
@ -36,7 +36,7 @@
</span> </span>
</div> </div>
</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="field is-grouped">
<div class="control"> <div class="control">
<button type="submit" class="button is-link">{{ $t('twofaccounts.forms.save') }}</button> <button type="submit" class="button is-link">{{ $t('twofaccounts.forms.save') }}</button>
@ -63,7 +63,7 @@
}, },
twofaccountExists: false, twofaccountExists: false,
tempIcon: '', tempIcon: '',
errors: {} validationErrors: {}
} }
}, },
@ -86,9 +86,12 @@
this.tempIcon = this.twofaccount.icon this.tempIcon = this.twofaccount.icon
}) })
.catch(error => { .catch(error => {
if (error.response.status === 404) { if( error.response.status === 404 ) {
this.$router.push({ name: '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 }}); this.$router.push({name: 'accounts', params: { InitialEditMode: true }});
}) })
.catch(error => { .catch(error => {
if (error.response.status === 400) { if (error.response.status === 404) {
this.errors = error.response.data.error
}
else if (error.response.status === 404) {
this.$router.push({ name: '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 => { .then(response => {
console.log('icon path > ', response); console.log('icon path > ', response);
this.tempIcon = response.data; this.tempIcon = response.data;
this.errors['icon'] = ''; this.validationErrors['icon'] = '';
}) })
.catch(error => { .catch(error => {
if (error.response.status === 400) { if( error.response.data.validation ) {
this.errors = error.response.data.error this.validationErrors = error.response.data.validation
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
} }
}); });
}, },