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()) {
return response()->json(['error' => $validator->errors()], 400);
return response()->json(['validation' => $validator->errors()], 400);
}
$path = $request->file('icon')->storePublicly('public/icons');

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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 } });
}
});

View File

@ -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 } });
}
});
},