Skip to content

Commit a2e8717

Browse files
committed
Handle submission error on form submit
1 parent e7335a4 commit a2e8717

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

templates/vue/api/fooFetch.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import SubmissionError from '../error/SubmissionError';
12
import { API_HOST, API_PATH } from './_entrypoint';
23

34
const jsonLdMimeType = 'application/ld+json';
@@ -23,6 +24,8 @@ export default function {{{ lc }}}Fetch(url, options = {}) {
2324

2425
let errors = {_error: error};
2526
json.violations.map((violation) => errors[violation.propertyPath] = violation.message);
27+
28+
throw new SubmissionError(errors);
2629
});
2730
});
2831
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Note : This work due to babel-plugin-transform-builtin-extend
2+
export default class SubmissionError extends Error {
3+
constructor(errors) {
4+
super('Submit Validation Failed');
5+
6+
this.errors = errors;
7+
8+
Error.captureStackTrace(this, this.constructor);
9+
10+
this.name = this.constructor.name;
11+
12+
return this;
13+
}
14+
}

templates/vue/store/modules/foo/create.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import SubmissionError from '../../../error/SubmissionError';
12
import {{{ lc }}}Fetch from '../../../api/{{{ lc }}}Fetch';
23

34
const {{{ uc }}}_CREATE_ERROR = '{{{ uc }}}_CREATE_ERROR';
45
const {{{ uc }}}_CREATE_LOADING = '{{{ uc }}}_CREATE_LOADING';
56
const {{{ uc }}}_CREATE_SUCCESS = '{{{ uc }}}_CREATE_SUCCESS';
7+
const {{{ uc }}}_CREATE_VIOLATIONS = '{{{ uc }}}_CREATE_VIOLATIONS';
68

79
const state = {
810
loading: false,
911
error: '',
10-
created: null
12+
created: null,
13+
violations: null
1114
};
1215

1316
function error(commit, error) {
@@ -22,10 +25,19 @@ function success(commit, created) {
2225
return commit({{{ uc }}}_CREATE_SUCCESS, created);
2326
}
2427

25-
const getters = {};
28+
function violations(commit, violations) {
29+
return commit(GATEWAY_CREATE_VIOLATIONS, violations);
30+
}
31+
32+
const getters = {
33+
created: state => state.created,
34+
error: state => state.error,
35+
loading: state => state.loading,
36+
violations: state => state.violations,
37+
};
2638

2739
const actions = {
28-
create({ commit }) {
40+
create({ commit }, values) {
2941
loading(commit, true);
3042

3143
return {{{ lc }}}Fetch('/{{{ name }}}', {method: 'POST', body: JSON.stringify(values)})
@@ -39,6 +51,13 @@ const actions = {
3951
})
4052
.catch(e => {
4153
loading(commit, false);
54+
55+
if (e instanceof SubmissionError) {
56+
violations(commit, e.errors);
57+
error(commit, e.errors._error);
58+
return;
59+
}
60+
4261
error(commit, e.message);
4362
});
4463
}
@@ -53,6 +72,9 @@ const mutations = {
5372
},
5473
[{{{ uc }}}_CREATE_SUCCESS] (state, created) {
5574
state.created = created;
75+
},
76+
[{{{ uc }}}_CREATE_VIOLATIONS] (state, violations) {
77+
state.violations = violations;
5678
}
5779
};
5880

0 commit comments

Comments
 (0)