-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1e6a1b
commit 6ce6c73
Showing
7 changed files
with
325 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 221 additions & 0 deletions
221
src-lookup/main/default/lwc/ldsUtils/__tests__/ldsUtils.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
import { reduceErrors } from "c/ldsUtils"; | ||
|
||
describe("c-lds-utils", () => { | ||
describe("reduceErrors", () => { | ||
it("reduces single error with message in body", () => { | ||
const FULL_ERROR = { body: { message: "mockError" } }; | ||
const REDUCED_ERROR = [FULL_ERROR.body.message]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces single error with multiple bodies with messages", () => { | ||
const FULL_ERROR = { | ||
body: [{ message: "mockError1" }, { message: "mockError2" }] | ||
}; | ||
const REDUCED_ERROR = [ | ||
FULL_ERROR.body[0].message, | ||
FULL_ERROR.body[1].message | ||
]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces single error message string", () => { | ||
const FULL_ERROR = { message: "mockError" }; | ||
const REDUCED_ERROR = [FULL_ERROR.message]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces array of error message string", () => { | ||
const FULL_ERROR = [{ message: "mockError1" }, { message: "mockError2" }]; | ||
const REDUCED_ERROR = [FULL_ERROR[0].message, FULL_ERROR[1].message]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces single UI API fieldError with error message string", () => { | ||
const FULL_ERROR = [ | ||
{ | ||
body: { | ||
output: { | ||
fieldErrors: { field1: [{ message: "mockError1" }] } | ||
} | ||
} | ||
} | ||
]; | ||
const REDUCED_ERROR = [ | ||
FULL_ERROR[0].body.output.fieldErrors.field1[0].message | ||
]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces array of UI API fieldErrors with error message string", () => { | ||
const FULL_ERROR = [ | ||
{ | ||
body: { | ||
output: { | ||
fieldErrors: { | ||
field1: [{ message: "mockError1" }], | ||
field2: [{ message: "mockError2" }] | ||
} | ||
} | ||
} | ||
} | ||
]; | ||
const REDUCED_ERROR = [ | ||
FULL_ERROR[0].body.output.fieldErrors.field1[0].message, | ||
FULL_ERROR[0].body.output.fieldErrors.field2[0].message | ||
]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces UI API single page level error with error message string", () => { | ||
const FULL_ERROR = { | ||
body: { | ||
output: { | ||
errors: [ | ||
{ | ||
message: "mockError" | ||
} | ||
] | ||
} | ||
} | ||
}; | ||
|
||
const REDUCED_ERROR = [FULL_ERROR.body.output.errors[0].message]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces UI API multiple page level error with error message string", () => { | ||
const FULL_ERROR = { | ||
body: { | ||
output: { | ||
errors: [ | ||
{ | ||
message: "mockError1" | ||
}, | ||
{ | ||
message: "mockError2" | ||
} | ||
] | ||
} | ||
} | ||
}; | ||
|
||
const REDUCED_ERROR = [ | ||
FULL_ERROR.body.output.errors[0].message, | ||
FULL_ERROR.body.output.errors[1].message | ||
]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces single page level error with error message string", () => { | ||
const FULL_ERROR = { | ||
body: { | ||
pageErrors: [ | ||
{ | ||
message: "mockError" | ||
} | ||
] | ||
} | ||
}; | ||
|
||
const REDUCED_ERROR = [FULL_ERROR.body.pageErrors[0].message]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces multiple page level error with error message string", () => { | ||
const FULL_ERROR = { | ||
body: { | ||
pageErrors: [ | ||
{ | ||
message: "mockError1" | ||
}, | ||
{ | ||
message: "mockError2" | ||
} | ||
] | ||
} | ||
}; | ||
|
||
const REDUCED_ERROR = [ | ||
FULL_ERROR.body.pageErrors[0].message, | ||
FULL_ERROR.body.pageErrors[1].message | ||
]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces single fieldError with error message string", () => { | ||
const FULL_ERROR = [ | ||
{ | ||
body: { | ||
fieldErrors: { field1: [{ message: "mockError1" }] } | ||
} | ||
} | ||
]; | ||
const REDUCED_ERROR = [FULL_ERROR[0].body.fieldErrors.field1[0].message]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces array of fieldErrors with error message string", () => { | ||
const FULL_ERROR = [ | ||
{ | ||
body: { | ||
fieldErrors: { | ||
field1: [{ message: "mockError1" }], | ||
field2: [{ message: "mockError2" }] | ||
} | ||
} | ||
} | ||
]; | ||
const REDUCED_ERROR = [ | ||
FULL_ERROR[0].body.fieldErrors.field1[0].message, | ||
FULL_ERROR[0].body.fieldErrors.field2[0].message | ||
]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
|
||
it("reduces single error with unknown shape", () => { | ||
const FULL_ERROR = { statusText: "mockStatus" }; | ||
const REDUCED_ERROR = [FULL_ERROR.statusText]; | ||
|
||
const reduced = reduceErrors(FULL_ERROR); | ||
|
||
expect(reduced).toStrictEqual(REDUCED_ERROR); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Reduces one or more LDS errors into a string[] of error messages. | ||
* @param {FetchResponse|FetchResponse[]} errors | ||
* @return {String[]} Error messages | ||
*/ | ||
export function reduceErrors(errors) { | ||
if (!Array.isArray(errors)) { | ||
errors = [errors]; | ||
} | ||
|
||
return ( | ||
errors | ||
// Remove null/undefined items | ||
.filter((error) => !!error) | ||
// Extract an error message | ||
.map((error) => { | ||
// UI API read errors | ||
if (Array.isArray(error.body)) { | ||
return error.body.map((e) => e.message); | ||
} | ||
// Page level errors | ||
else if (error?.body?.pageErrors && error.body.pageErrors.length > 0) { | ||
return error.body.pageErrors.map((e) => e.message); | ||
} | ||
// Field level errors | ||
else if ( | ||
error?.body?.fieldErrors && | ||
Object.keys(error.body.fieldErrors).length > 0 | ||
) { | ||
const fieldErrors = []; | ||
Object.values(error.body.fieldErrors).forEach((errorArray) => { | ||
fieldErrors.push(...errorArray.map((e) => e.message)); | ||
}); | ||
return fieldErrors; | ||
} | ||
// UI API DML page level errors | ||
else if ( | ||
error?.body?.output?.errors && | ||
error.body.output.errors.length > 0 | ||
) { | ||
return error.body.output.errors.map((e) => e.message); | ||
} | ||
// UI API DML field level errors | ||
else if ( | ||
error?.body?.output?.fieldErrors && | ||
Object.keys(error.body.output.fieldErrors).length > 0 | ||
) { | ||
const fieldErrors = []; | ||
Object.values(error.body.output.fieldErrors).forEach((errorArray) => { | ||
fieldErrors.push(...errorArray.map((e) => e.message)); | ||
}); | ||
return fieldErrors; | ||
} | ||
// UI API DML, Apex and network errors | ||
else if (error.body && typeof error.body.message === "string") { | ||
return error.body.message; | ||
} | ||
// JS errors | ||
else if (typeof error.message === "string") { | ||
return error.message; | ||
} | ||
// Unknown error shape so try HTTP status text | ||
return error.statusText; | ||
}) | ||
// Flatten | ||
.reduce((prev, curr) => prev.concat(curr), []) | ||
// Remove empty strings | ||
.filter((message) => !!message) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>56.0</apiVersion> | ||
<isExposed>false</isExposed> | ||
</LightningComponentBundle> |
6 changes: 6 additions & 0 deletions
6
src-lookup/main/default/objects/Lookup__mdt/Lookup__mdt.object-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<label>Lookup</label> | ||
<pluralLabel>Lookups</pluralLabel> | ||
<visibility>Public</visibility> | ||
</CustomObject> |
11 changes: 11 additions & 0 deletions
11
src-lookup/main/default/objects/Lookup__mdt/fields/Interface__c.field-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<fullName>Interface__c</fullName> | ||
<externalId>false</externalId> | ||
<fieldManageability>DeveloperControlled</fieldManageability> | ||
<label>Interface</label> | ||
<length>255</length> | ||
<required>true</required> | ||
<type>Text</type> | ||
<unique>false</unique> | ||
</CustomField> |