Skip to content

Commit 93403c8

Browse files
Handle form errors (#270)
* removed useless import in Show * display api error for each fields * refacto with reduce * reverted ReferenceLinks import suppression * removed useless SubmissionError class, set errors Formik compatible in dataAccess.ts * tests passed * simplified errors * added ErrorMessage component * set default msg * typed Violation
1 parent 8b60676 commit 93403c8

File tree

5 files changed

+16
-41
lines changed

5 files changed

+16
-41
lines changed

src/generators/NextGenerator.js

-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ export default class NextGenerator extends BaseGenerator {
1313
"components/foo/Show.tsx",
1414
"components/foo/Form.tsx",
1515

16-
// interfaces
17-
"error/SubmissionError.ts",
18-
1916
// types
2017
"types/Collection.ts",
2118
"types/foo.ts",
@@ -92,9 +89,6 @@ export default class NextGenerator extends BaseGenerator {
9289
// components
9390
"components/common/ReferenceLinks.tsx",
9491

95-
// error
96-
"error/SubmissionError.ts",
97-
9892
// types
9993
"types/Collection.ts",
10094

src/generators/NextGenerator.test.js

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ describe("generate", () => {
4444
"/components/abc/Show.tsx",
4545
"/components/abc/Form.tsx",
4646
"/components/common/ReferenceLinks.tsx",
47-
"/error/SubmissionError.ts",
4847
"/types/Abc.ts",
4948
"/types/Collection.ts",
5049
"/pages/abcs/[id]/index.tsx",

templates/next/components/foo/Form.tsx

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FunctionComponent, useState } from "react";
22
import Link from "next/link";
33
import { useRouter } from "next/router";
44
import Head from "next/head";
5-
import { Formik } from "formik";
5+
import { ErrorMessage, Formik } from "formik";
66
import { fetch } from "../../utils/dataAccess";
77
import { {{{ucf}}} } from '../../types/{{{ucf}}}';
88

@@ -21,7 +21,7 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
2121
await fetch({{{lc}}}['@id'], { method: "DELETE" });
2222
router.push("/{{{name}}}");
2323
} catch (error) {
24-
setError("Error when deleting the resource.");
24+
setError(`Error when deleting the resource: ${error}`);
2525
console.error(error);
2626
}
2727
};
@@ -42,7 +42,7 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
4242
// add your validation logic here
4343
return errors;
4444
}}
45-
onSubmit={async (values, { setSubmitting, setStatus }) => {
45+
onSubmit={async (values, { setSubmitting, setStatus, setErrors }) => {
4646
const isCreation = !values["@id"];
4747
try {
4848
await fetch(isCreation ? "/{{{name}}}" : values["@id"], {
@@ -57,8 +57,9 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
5757
} catch (error) {
5858
setStatus({
5959
isValid: false,
60-
msg: `Error when ${isCreation ? 'creating': 'updating'} the resource.`,
60+
msg: `${error.defaultErrorMsg}`,
6161
});
62+
setErrors(error.fields);
6263
}
6364
setSubmitting(false);
6465
}}
@@ -91,7 +92,11 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
9192
onBlur={handleBlur}
9293
/>
9394
</div>
94-
{ errors.{{name}} && touched.{{name}} && <div className="invalid-feedback">{ errors.{{name}} }</div> }
95+
<ErrorMessage
96+
className="text-danger"
97+
component="div"
98+
name="{{name}}"
99+
/>
95100
{{/each}}
96101

97102
{status && status.msg && (
@@ -105,12 +110,6 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
105110
</div>
106111
)}
107112

108-
{error && (
109-
<div className="alert alert-danger" role="alert">
110-
{error}
111-
</div>
112-
)}
113-
114113
<button
115114
type="submit"
116115
className="btn btn-success"

templates/next/error/SubmissionError.ts

-16
This file was deleted.

templates/next/utils/dataAccess.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import has from "lodash/has";
33
import mapValues from "lodash/mapValues";
44
import isomorphicFetch from "isomorphic-unfetch";
55
import { ENTRYPOINT } from "../config/entrypoint";
6-
import { SubmissionError, SubmissionErrorList } from "../error/SubmissionError";
76

87
const MIME_TYPE = "application/ld+json";
98

@@ -29,16 +28,16 @@ export const fetch = async (id: string, init: RequestInit = {}) => {
2928
const json = await resp.json();
3029
if (resp.ok) return normalize(json);
3130

32-
const error = json["{{{hydraPrefix}}}description"] || resp.statusText;
33-
if (!json.violations) throw Error(error);
34-
35-
const errors: SubmissionErrorList = { _error: error };
31+
const defaultErrorMsg = json["hydra:title"];
32+
const status = json["hydra:description"] || resp.statusText;
33+
if (!json.violations) throw Error(defaultErrorMsg);
34+
const fields = {};
3635
json.violations.map(
3736
(violation: Violation) =>
38-
(errors[violation.propertyPath] = violation.message)
37+
(fields[violation.propertyPath] = violation.message)
3938
);
4039

41-
throw new SubmissionError(errors);
40+
throw { defaultErrorMsg, status, fields };
4241
};
4342

4443
export const normalize = (data: any) => {

0 commit comments

Comments
 (0)