Skip to content

Commit

Permalink
Failed-record errors from "message" instead of "label" (#71)
Browse files Browse the repository at this point in the history
When a error structure has a `message` as well as a `label`, use the former rather than the latter. We still fall back to the label when there is no message.

Add tests for both cases.

These are in fact the first tests in the module, so now for the first time it's possible to run `yarn test`. A little bit of infrastructure was added to support the use of Jest.

Fixes UIHAADM-125.
  • Loading branch information
MikeTaylor authored May 27, 2024
1 parent 3a1fdde commit 0577144
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
junit.xml
node_modules
yarn.lock
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change history for ui-harvester-admin

## 2.2.0 (IN PROGRESS)

* Failed-record error messages should use detailed "message", when present, instead of label. Fixes UIHAADM-125.

## [2.1.0](https://github.com/folio-org/ui-harvester-admin/tree/v2.1.0) (2024-02-28)

* Sort jobs in descending order by start date. Fixes UIHAADM-94.
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@folio/jest-config-stripes');
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"main": "src/index.js",
"scripts": {
"start": "stripes serve --port 3003 --okapi https://harvester-dev-okapi.folio-dev.indexdata.com --tenant harvester",
"test": "jest",
"minerva3": "stripes serve --port 3004 --okapi https://okapi.gbv.de --tenant minerva3",
"lint": "eslint -f unix .",
"qlint": "eslint -f unix --rule '{\"no-console\":\"off\"}' .",
Expand Down
8 changes: 6 additions & 2 deletions src/util/summarizeErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ function errors2react(errors) {
errors.map(error => {
const message = error.error?.message;
const errorList = message?.errors;
const mm = message.message;
const mm = message?.message;

if (errorList) {
return errorList.map((x, i) => <li key={i}>{x.message}</li>);
} else if (mm) {
return mm;
} else if (error.error?.message) {
return error.error?.message;
} else {
return error.error?.label;
}
Expand All @@ -28,12 +30,14 @@ function errors2string(errors) {
errors.map(error => {
const message = error.error?.message;
const errorList = message?.errors;
const mm = message.message;
const mm = message?.message;

if (errorList) {
return errorList.map(x => x.message).join(' / ');
} else if (mm) {
return mm;
} else if (error.error?.message) {
return error.error?.message;
} else {
return error.error?.label;
}
Expand Down
29 changes: 29 additions & 0 deletions src/util/summarizeErrors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { errors2string } from './summarizeErrors';

const errorFromMessage = `[
{
"error": {
"label": "Error encountered during upsert of Inventory record set",
"message": "ERROR: Cannot update record c352fe11-51e2-4e8b-87d6-578d6dedec8b because it has been changed (optimistic locking): Stored _version is 4, _version of request is 1 (23F09)"
}
}
]`;

const errorFromLabel = `[
{
"error": {
"label": "Error encountered during upsert of Inventory record set"
}
}
]`;


test('summarizes simple error from message', () => {
const errorsData = JSON.parse(errorFromMessage);
expect(errors2string(errorsData)).toBe('ERROR: Cannot update record c352fe11-51e2-4e8b-87d6-578d6dedec8b because it has been changed (optimistic locking): Stored _version is 4, _version of request is 1 (23F09)');
});

test('summarizes simple error from label', () => {
const errorsData = JSON.parse(errorFromLabel);
expect(errors2string(errorsData)).toBe('Error encountered during upsert of Inventory record set');
});

0 comments on commit 0577144

Please sign in to comment.