From 0a98eef265817dfbc259105151795a5fc44e1ebb Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 8 Oct 2020 11:15:07 +1100 Subject: [PATCH] core: improved form data handling and better reporting of internal errors during rest operations. --- packages/core/src/rest_client/fetch-api.js | 5 +++- packages/core/src/rest_client/get.js | 4 +-- packages/core/src/rest_client/postPut.js | 27 +++++++++++++++++-- packages/core/src/rest_client/rest.actions.js | 6 ++--- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/packages/core/src/rest_client/fetch-api.js b/packages/core/src/rest_client/fetch-api.js index 74a1d2a..3ab1973 100644 --- a/packages/core/src/rest_client/fetch-api.js +++ b/packages/core/src/rest_client/fetch-api.js @@ -109,7 +109,10 @@ export function fetchApi(apiUrl, request, silent) { const tmpRequest = request; let contentType = null; if (tmpRequest && tmpRequest.body && typeof (tmpRequest.body) !== 'string') { - if (method === 'GET') { + // eslint-disable-next-line no-undef + if (tmpRequest.body instanceof FormData) { + contentType = undefined; // 'multipart/form-data; boundary=`'; + } else if (method === 'GET') { tmpRequest.body = JSON.stringify(tmpRequest.body); contentType = 'application/json'; } else { diff --git a/packages/core/src/rest_client/get.js b/packages/core/src/rest_client/get.js index 13d7fe8..dfe2839 100644 --- a/packages/core/src/rest_client/get.js +++ b/packages/core/src/rest_client/get.js @@ -107,7 +107,7 @@ export function buildURL(parts, vars) { if (!v) { throw Error(`Missing assignment to url parameter '${k}'`); } - id.forEach((i) => { a2[i] = encodeURI(v); }); + id.forEach((i) => { a2[i] = encodeURIComponent(v); }); }); return a2; } @@ -122,7 +122,7 @@ export function buildURL(parts, vars) { } const qe = Object.entries(qph); if (qe.length > 0) { - const query = qe.map(([k, v]) => `${encodeURI(k)}=${encodeURI(v)}`).join('&'); + const query = qe.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&'); return `${path}?${query}`; } else { return path; diff --git a/packages/core/src/rest_client/postPut.js b/packages/core/src/rest_client/postPut.js index 03139f0..1941ff1 100644 --- a/packages/core/src/rest_client/postPut.js +++ b/packages/core/src/rest_client/postPut.js @@ -49,6 +49,7 @@ const registerMethod = (method, opts) => { const submitType = `${ACTION_TYPES[`${method}_SUBMITTED`]}:${name}`; const resultType = `${ACTION_TYPES[`${method}_RESULT`]}:${name}`; const errorType = `${ACTION_TYPES[`${method}_ERROR`]}:${name}`; + const intErrorType = `${ACTION_TYPES[`${method}_INTERNAL_ERROR`]}:${name}`; registerReducer(trigger, (state, action) => { if (guard) { @@ -56,10 +57,32 @@ const registerMethod = (method, opts) => { return state; } } - const [body, vars] = request(action, state, variables); + let r; + try { + r = request(action, state, variables); + } catch (e) { + dispatchFromReducer({ + type: intErrorType, + call: 'request', + // eslint-disable-next-line object-property-newline + action, state, variables, + error: e, + }); + } + const [body, vars] = r; if (body) { const url2 = buildURL(parts, vars, variables); - runMethod(method, url2, name, body, vars, resultType, errorType, action); + try { + runMethod(method, url2, name, body, vars, resultType, errorType, action); + } catch (e) { + dispatchFromReducer({ + type: intErrorType, + call: 'runMethod', + // eslint-disable-next-line object-property-newline + method, url2, name, body, vars, resultType, errorType, action, + error: e, + }); + } // eslint-disable-next-line object-curly-newline dispatchFromReducer({ type: submitType, diff --git a/packages/core/src/rest_client/rest.actions.js b/packages/core/src/rest_client/rest.actions.js index 99683ed..df11b06 100644 --- a/packages/core/src/rest_client/rest.actions.js +++ b/packages/core/src/rest_client/rest.actions.js @@ -4,10 +4,10 @@ const Domain = 'REST'; export const ACTION_TYPES = registerActions(Domain, [ 'THROW_UNAUTHORISED_ERROR', 'THROW_PERMISSION_DENIED_ERROR', - 'GET_SUBMITTED', 'GET_RESULT', 'GET_ERROR', + 'GET_SUBMITTED', 'GET_RESULT', 'GET_ERROR', 'GET_INTERNAL_ERROR', 'GET_PERIODIC_TICK', - 'POST_SUBMITTED', 'POST_RESULT', 'POST_ERROR', - 'PUT_SUBMITTED', 'PUT_RESULT', 'PUT_ERROR', + 'POST_SUBMITTED', 'POST_RESULT', 'POST_ERROR', 'POST_INTERNAL_ERROR', + 'PUT_SUBMITTED', 'PUT_RESULT', 'PUT_ERROR', 'PUT_INTERNAL_ERROR', ]); export function throwUnauthorisedError() {