Skip to content

Commit

Permalink
ui: drafts - fix error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Pamfilos Fokianos <[email protected]>
  • Loading branch information
pamfilos committed Nov 23, 2020
1 parent 5afcfa8 commit ac8dfee
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 42 deletions.
60 changes: 47 additions & 13 deletions ui/cap-react/src/actions/draftItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,10 @@ export function reviewDraft(draft_id, review, message = "submitted") {

export function updateDraft(data, draft_id) {
return dispatch => {
return dispatch(putUpdateDraft(data, draft_id)).catch(error => {
dispatch(updateDraftError(error.response));
throw error;
});
return dispatch(putUpdateDraft(data, draft_id))
.catch(error => {
dispatch(updateDraftError(error));
});
};
}

Expand All @@ -504,25 +504,60 @@ export function putUpdateDraft(data, draft_id) {
.then(response => {
dispatch(updateDraftSuccess(draft_id, response.data));

cogoToast.success("Your Draft has been successfully submitted", {
cogoToast.success("Your Draft has been updated successfully", {
position: "top-center",
heading: "Draft saved",
bar: { size: "0" },
hideAfter: 3
});
})
.catch(error => {
dispatch(updateDraftError(error));
let errorHeading = "Error while updating";
let errorDescription;
let errorHideAfter = 6;
let errorThrow = "Error while updating";

if (error.response.status == 422) {
let _errors = error.response.data.errors;
let errorTree = {};
_errors.map(e => {
let tmp = errorTree;
e.field.map(field => {
if (!tmp[field]) tmp[field] = {}
tmp = tmp[field];
});

if (!tmp["__errors"]) tmp["__errors"] = []
tmp["__errors"].push(e.message);
});

dispatch(formErrorsChange(_toErrorList(errorTree)));
errorHeading = "Validation Error while updating";
errorDescription = "Please fix the errors before saving again";
errorThrow = errorTree;
}
else if (error.response.status == 403 && error.response.data && error.response.data.message == "Invalid action") {
errorDescription = "Either you need permissions or you are trying to publish an already published item";
}
else if (error.response) {
errorDescription = error.response.data && error.response.data.message ?
error.response.data.message : "";
} else if (error.request) {
// client never received a response, or request never left
errorHeading = "Something went wrong";
errorDescription = "There is an error, please make sure you are connected and try again";
} else {
// anything else
}
cogoToast.error(
"There is an error, please make sure you are connected and try again",
errorDescription,
{
position: "top-center",
heading: error.message,
heading: errorHeading,
bar: { size: "0" },
hideAfter: 3
}
);
throw error;
hideAfter: errorHideAfter
});
throw errorThrow;
});
};
}
Expand Down Expand Up @@ -629,7 +664,6 @@ export function publishDraft(draft_id) {
})
.catch(error => {
dispatch(publishDraftError(error));
// throw error;
});
};
}
Expand Down
1 change: 1 addition & 0 deletions ui/cap-react/src/components/drafts/DraftEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ DraftEditor.propTypes = {
schemaErrors: PropTypes.array,
// schemaId: PropTypes.string,
error: PropTypes.object,
extraErrors: PropTypes.object,
formData: PropTypes.object,
draft_id: PropTypes.string,
formDataChange: PropTypes.func,
Expand Down
24 changes: 2 additions & 22 deletions ui/cap-react/src/components/drafts/form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,6 @@ class DepositForm extends Component {
};
}

_validate(formData, errors) {
// Example on how to add custom errors
// "errors" object is a representation of the schema field tree
// with an "addError" function attached to each property
// e.g.
// errors.basic_info.cadi_id.addError("boom");

// TODO: Updates error list with ASYNC ERRORs
if (!Array.isArray(this.props.errors)) return errors;

this.props.errors.map(error => {
if (error.field) {
let errorObj = objectPath.get(errors, error.field);
errorObj.addError(error.message);
}
});

return errors;
}

transformErrors = errors => {
errors = errors
.filter((item) => item.name != "required");
Expand Down Expand Up @@ -83,7 +63,7 @@ class DepositForm extends Component {
uiSchema={this.props.uiSchema}
liveValidate={false}
noValidate={false}
validate={this._validate.bind(this)}
validate={this.props.validate}
onError={() => { }}
transformErrors={this.transformErrors}
formData={this.props.formData}
Expand Down Expand Up @@ -116,7 +96,7 @@ DepositForm.propTypes = {
onChange: PropTypes.func,
errors: PropTypes.array,
formRef: PropTypes.object,
extraErrors: PropTypes.array,
extraErrors: PropTypes.object,
formContext: PropTypes.object
};

Expand Down
11 changes: 4 additions & 7 deletions ui/cap-react/src/reducers/draftItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default function draftsReducer(state = initialState, action) {
.set("formErrors", Set([]))
.set("extraErrors", {});
case commonActions.FORM_ERRORS:
return state.set("formErrors", Set(action.errors));
return state.set("formErrors", state.get('formErrors').union(action.errors));
case commonActions.FETCH_SCHEMA_ERROR:
return state.set("schemaErrors", [
...state.get("schemaErrors"),
Expand Down Expand Up @@ -120,20 +120,17 @@ export default function draftsReducer(state = initialState, action) {
return state
.set("formData", action.draft.metadata)
.set("loading", false)
.set("formErrors", Set([]))
.set("extraErrors", {})
.set("message", {
status: "ok",
msg: "All changes saved"
})
.merge(Map(action.draft));
case draftItemActions.UPDATE_DRAFT_ERROR:
return state
.set("extraErrors", action.error)
.set("loading", false)
.set("errors", [...state.get("errors"), action.error])
.set("message", {
status: "critical",
msg: "Error while updating.."
});

case draftItemActions.DELETE_DRAFT_REQUEST:
return state.set("loading", true);
case draftItemActions.DELETE_DRAFT_SUCCESS:
Expand Down

0 comments on commit ac8dfee

Please sign in to comment.