Skip to content

Commit 65e22a5

Browse files
committed
Update certain exports and imports to return partial results
1 parent ebf6df8 commit 65e22a5

18 files changed

+547
-337
lines changed

src/api/AmConfigApi.ts

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { FrodoError } from '../ops/FrodoError';
12
import Constants from '../shared/Constants';
23
import { State } from '../shared/State';
3-
import { printError, printMessage } from '../utils/Console';
44
import {
55
getRealmPathGlobal,
66
getRealmsForExport,
@@ -259,11 +259,10 @@ export async function getConfigEntity({
259259
state.setRealm(currentRealm);
260260
return data;
261261
} catch (error) {
262-
printError({
263-
error,
264-
message: `Error getting config entity from resource path '${urlString}'`,
265-
state,
266-
});
262+
throw new FrodoError(
263+
`Error getting config entity from resource path '${urlString}'`,
264+
error
265+
);
267266
}
268267
}
269268

@@ -291,6 +290,7 @@ export async function getConfigEntities({
291290
global: {},
292291
realm: Object.fromEntries(realms.map((r) => [r, {}])),
293292
} as ConfigSkeleton;
293+
const errors = [];
294294
for (const [key, entityInfo] of Object.entries(AM_ENTITIES)) {
295295
if (!includeReadOnly && entityInfo.readonly) {
296296
continue;
@@ -319,11 +319,12 @@ export async function getConfigEntities({
319319
: entityInfo.action,
320320
});
321321
} catch (e) {
322-
printMessage({
323-
message: `Error getting '${key}' from resource path '${entityInfo.global.path}': ${e.message}`,
324-
type: 'error',
325-
state,
326-
});
322+
errors.push(
323+
new FrodoError(
324+
`Error getting '${key}' from resource path '${entityInfo.global.path}': ${e.message}`,
325+
e
326+
)
327+
);
327328
}
328329
}
329330
if (
@@ -357,15 +358,19 @@ export async function getConfigEntities({
357358
: entityInfo.action,
358359
});
359360
} catch (e) {
360-
printMessage({
361-
message: `Error getting '${key}' from resource path '${entityInfo.realm.path}': ${e.message}`,
362-
type: 'error',
363-
state,
364-
});
361+
errors.push(
362+
new FrodoError(
363+
`Error getting '${key}' from resource path '${entityInfo.realm.path}': ${e.message}`,
364+
e
365+
)
366+
);
365367
}
366368
}
367369
}
368370
}
371+
if (errors.length > 0) {
372+
throw new FrodoError('Errors getting config entities', errors, entities);
373+
}
369374
return entities;
370375
}
371376

@@ -418,11 +423,10 @@ export async function putConfigEntity({
418423
state.setRealm(currentRealm);
419424
return data;
420425
} catch (error) {
421-
printError({
422-
error,
423-
message: `Error putting config entity at resource path '${urlString}'`,
424-
state,
425-
});
426+
throw new FrodoError(
427+
`Error putting config entity at resource path '${urlString}'`,
428+
error
429+
);
426430
}
427431
}
428432

@@ -444,6 +448,7 @@ export async function putConfigEntities({
444448
global: {},
445449
realm: Object.fromEntries(realms.map((r) => [r, {}])),
446450
} as ConfigSkeleton;
451+
const errors = [];
447452
for (const [key, entityInfo] of Object.entries(AM_ENTITIES)) {
448453
if (entityInfo.readonly) {
449454
continue;
@@ -459,11 +464,11 @@ export async function putConfigEntities({
459464
config.global &&
460465
config.global[key]
461466
) {
462-
try {
463-
for (const [id, entityData] of Object.entries(config.global[key])) {
464-
if (!entities.global[key]) {
465-
entities.global[key] = {};
466-
}
467+
for (const [id, entityData] of Object.entries(config.global[key])) {
468+
if (!entities.global[key]) {
469+
entities.global[key] = {};
470+
}
471+
try {
467472
entities.global[key][id] = await putConfigEntity({
468473
state,
469474
entityData: entityData as ConfigEntitySkeleton,
@@ -474,13 +479,14 @@ export async function putConfigEntities({
474479
protocol: entityInfo.global.protocol,
475480
ifMatch: entityInfo.global.ifMatch,
476481
});
482+
} catch (e) {
483+
errors.push(
484+
new FrodoError(
485+
`Error putting entity '${id}' of type '${key}' to global resource path '${entityInfo.global.path}': ${e.message}`,
486+
e
487+
)
488+
);
477489
}
478-
} catch (e) {
479-
printMessage({
480-
message: `Error putting '${key}' from resource path '${entityInfo.global.path}': ${e.message}`,
481-
type: 'error',
482-
state,
483-
});
484490
}
485491
}
486492
if (
@@ -493,13 +499,13 @@ export async function putConfigEntities({
493499
if (!config.realm[realms[i]][key]) {
494500
continue;
495501
}
496-
try {
497-
for (const [id, entityData] of Object.entries(
498-
config.realm[realms[i]][key]
499-
)) {
500-
if (!entities.realm[realms[i]][key]) {
501-
entities.realm[realms[i]][key] = {};
502-
}
502+
for (const [id, entityData] of Object.entries(
503+
config.realm[realms[i]][key]
504+
)) {
505+
if (!entities.realm[realms[i]][key]) {
506+
entities.realm[realms[i]][key] = {};
507+
}
508+
try {
503509
entities.realm[realms[i]][key][id] = await putConfigEntity({
504510
state,
505511
entityData: entityData as ConfigEntitySkeleton,
@@ -511,16 +517,19 @@ export async function putConfigEntities({
511517
ifMatch: entityInfo.realm.ifMatch,
512518
realm: stateRealms[i],
513519
});
520+
} catch (e) {
521+
errors.push(
522+
new FrodoError(
523+
`Error putting entity '${id}' of type '${key}' to realm resource path '${entityInfo.realm.path}': ${e.message}`
524+
)
525+
);
514526
}
515-
} catch (e) {
516-
printMessage({
517-
message: `Error putting '${key}' from resource path '${entityInfo.realm.path}': ${e.message}`,
518-
type: 'error',
519-
state,
520-
});
521527
}
522528
}
523529
}
524530
}
531+
if (errors.length > 0) {
532+
throw new FrodoError('Errors putting config entities', errors, entities);
533+
}
525534
return entities;
526535
}

src/ops/AmConfigOps.test.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { filterRecording } from "../utils/PollyUtils";
5050
import * as AmConfigOps from "./AmConfigOps";
5151
import { state } from "../lib/FrodoLib";
5252
import Constants from "../shared/Constants";
53+
import { getResults } from "../utils/ExportImportUtils";
5354

5455
const ctx = autoSetupPolly();
5556

@@ -98,31 +99,35 @@ describe('AmConfigOps', () => {
9899
});
99100

100101
test('1: Export AM Config Entities', async () => {
101-
const response = await AmConfigOps.exportAmConfigEntities({ includeReadOnly: true, onlyRealm: false, onlyGlobal: false, state });
102-
expect(response).toMatchSnapshot({
103-
meta: expect.any(Object),
102+
const results = await getResults(AmConfigOps.exportAmConfigEntities, { includeReadOnly: true, onlyRealm: false, onlyGlobal: false, state });
103+
expect(results.result).toMatchSnapshot({
104+
meta: expect.any(Object)
104105
});
106+
expect(results.error?.getCombinedMessage()).toMatchSnapshot();
105107
});
106108

107109
test('2: Export importable AM Config Entities', async () => {
108-
const response = await AmConfigOps.exportAmConfigEntities({ includeReadOnly: false, onlyRealm: false, onlyGlobal: false, state });
109-
expect(response).toMatchSnapshot({
110-
meta: expect.any(Object),
110+
const results = await getResults(AmConfigOps.exportAmConfigEntities, { includeReadOnly: false, onlyRealm: false, onlyGlobal: false, state });
111+
expect(results.result).toMatchSnapshot({
112+
meta: expect.any(Object)
111113
});
114+
expect(results.error?.getCombinedMessage()).toMatchSnapshot();
112115
});
113116

114117
test('3: Export alpha realm AM Config Entities', async () => {
115-
const response = await AmConfigOps.exportAmConfigEntities({ includeReadOnly: true, onlyRealm: true, onlyGlobal: false, state });
116-
expect(response).toMatchSnapshot({
117-
meta: expect.any(Object),
118+
const results = await getResults(AmConfigOps.exportAmConfigEntities, { includeReadOnly: true, onlyRealm: true, onlyGlobal: false, state });
119+
expect(results.result).toMatchSnapshot({
120+
meta: expect.any(Object)
118121
});
122+
expect(results.error?.getCombinedMessage()).toMatchSnapshot();
119123
});
120124

121125
test('4: Export global AM Config Entities', async () => {
122-
const response = await AmConfigOps.exportAmConfigEntities({ includeReadOnly: true, onlyRealm: false, onlyGlobal: true, state });
123-
expect(response).toMatchSnapshot({
124-
meta: expect.any(Object),
126+
const results = await getResults(AmConfigOps.exportAmConfigEntities, { includeReadOnly: true, onlyRealm: false, onlyGlobal: true, state });
127+
expect(results.result).toMatchSnapshot({
128+
meta: expect.any(Object)
125129
});
130+
expect(results.error?.getCombinedMessage()).toMatchSnapshot();
126131
});
127132
});
128133

@@ -149,31 +154,35 @@ describe('AmConfigOps', () => {
149154

150155
describe('exportAmConfigEntities()', () => {
151156
test('5: Export AM Config Entities', async () => {
152-
const response = await AmConfigOps.exportAmConfigEntities({ includeReadOnly: true, onlyRealm: false, onlyGlobal: false, state });
153-
expect(response).toMatchSnapshot({
154-
meta: expect.any(Object),
157+
const results = await getResults(AmConfigOps.exportAmConfigEntities, { includeReadOnly: true, onlyRealm: false, onlyGlobal: false, state });
158+
expect(results.result).toMatchSnapshot({
159+
meta: expect.any(Object)
155160
});
161+
expect(results.error?.getCombinedMessage()).toMatchSnapshot();
156162
});
157163

158164
test('6: Export importable AM Config Entities', async () => {
159-
const response = await AmConfigOps.exportAmConfigEntities({ includeReadOnly: false, onlyRealm: false, onlyGlobal: false, state });
160-
expect(response).toMatchSnapshot({
161-
meta: expect.any(Object),
165+
const results = await getResults(AmConfigOps.exportAmConfigEntities, { includeReadOnly: false, onlyRealm: false, onlyGlobal: false, state });
166+
expect(results.result).toMatchSnapshot({
167+
meta: expect.any(Object)
162168
});
169+
expect(results.error?.getCombinedMessage()).toMatchSnapshot();
163170
});
164171

165172
test('7: Export root realm AM Config Entities', async () => {
166-
const response = await AmConfigOps.exportAmConfigEntities({ includeReadOnly: true, onlyRealm: true, onlyGlobal: false, state });
167-
expect(response).toMatchSnapshot({
168-
meta: expect.any(Object),
173+
const results = await getResults(AmConfigOps.exportAmConfigEntities, { includeReadOnly: true, onlyRealm: true, onlyGlobal: false, state });
174+
expect(results.result).toMatchSnapshot({
175+
meta: expect.any(Object)
169176
});
177+
expect(results.error?.getCombinedMessage()).toMatchSnapshot();
170178
});
171179

172180
test('8: Export global AM Config Entities', async () => {
173-
const response = await AmConfigOps.exportAmConfigEntities({ includeReadOnly: true, onlyRealm: false, onlyGlobal: true, state });
174-
expect(response).toMatchSnapshot({
175-
meta: expect.any(Object),
181+
const results = await getResults(AmConfigOps.exportAmConfigEntities, { includeReadOnly: true, onlyRealm: false, onlyGlobal: true, state });
182+
expect(results.result).toMatchSnapshot({
183+
meta: expect.any(Object)
176184
});
185+
expect(results.error?.getCombinedMessage()).toMatchSnapshot();
177186
});
178187
});
179188

0 commit comments

Comments
 (0)