Skip to content

Commit

Permalink
fix #70: handle api errors
Browse files Browse the repository at this point in the history
  • Loading branch information
manojadams committed May 9, 2024
1 parent d1b324c commit eae007d
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/core/MetaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,27 @@ export default class MetaForm implements IMetaForm {
section,
// eslint-disable-next-line camelcase
config?.urlType === URL_TYPE.REMOTE
).then((response: object) => {
const responseKey = config?.responseKey;
const results = responseKey ? response[responseKey] : response;
let newResults: Array<IOption> = [];
if (Array.isArray(results)) {
const labelKey = config?.labelKey ?? "label";
const valueKey = config?.valueKey ?? "value";
newResults = results.map((r: string) => {
const label = FormUtils.getDataFromValueKey(r, labelKey);
const value = FormUtils.getDataFromValueKey(r, valueKey);
return { label, value, ref: r };
});
resolve(newResults);
} else {
reject(new MetaformError("Response must be an array", response));
}
});
)
.then((response: object) => {
const responseKey = config?.responseKey;
const results = responseKey ? response[responseKey] : response;
let newResults: Array<IOption> = [];
if (Array.isArray(results)) {
const labelKey = config?.labelKey ?? "label";
const valueKey = config?.valueKey ?? "value";
newResults = results.map((r: string) => {
const label = FormUtils.getDataFromValueKey(r, labelKey);
const value = FormUtils.getDataFromValueKey(r, valueKey);
return { label, value, ref: r };
});
resolve(newResults);
} else {
reject(new MetaformError("Response must be an array", response));
}
})
.catch((error) => {
reject(new MetaformError("Server error", error));
});
break;
}
}
Expand Down

0 comments on commit eae007d

Please sign in to comment.