Skip to content

Commit

Permalink
Get rid of error wrapping (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Dec 8, 2023
1 parent 49ebbaf commit b9a1542
Show file tree
Hide file tree
Showing 10 changed files with 2,020 additions and 1,838 deletions.
492 changes: 264 additions & 228 deletions cli/package-lock.json

Large diffs are not rendered by default.

437 changes: 205 additions & 232 deletions core/package-lock.json

Large diffs are not rendered by default.

33 changes: 8 additions & 25 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,15 @@ export class Seeder {
): SeederCollection[] => {
const config = mergeCollectionReadingOptions(partialConfig);
let collections;
try {
const { CollectionPopulator } = require('./populator');
const populator = new CollectionPopulator(
config.extensions,
config.ejsonParseOptions,
this.log,
);
const { CollectionPopulator } = require('./populator');
const populator = new CollectionPopulator(
config.extensions,
config.ejsonParseOptions,
this.log,
);

this.log(`Reading collections from ${path}...`);
collections = populator.readFromPath(path);
} catch (err) {
throw wrapError(err as Error);
}
this.log(`Reading collections from ${path}...`);
collections = populator.readFromPath(path);

if (config.transformers.length > 0) {
this.log('Transforming collections...');
Expand Down Expand Up @@ -138,8 +134,6 @@ export class Seeder {
config.bulkWriteOptions,
this.log,
).import(collections);
} catch (err) {
throw wrapError(err as Error);
} finally {
if (database) {
await database.closeConnection();
Expand All @@ -149,14 +143,3 @@ export class Seeder {
this.log('Finishing...');
};
}

/**
* Wraps error with custom name
*
* @param err Original error
*/
const wrapError = (err: Error) => {
err.name = 'MongoSeedingError';
err.message = `${err.name}: ${err.message}`;
return err;
};
13 changes: 9 additions & 4 deletions core/test/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,26 @@ describe('Mongo Seeding', () => {

it('should throw error when wrong path given', async () => {
const seeder = new Seeder();
await expect(() =>

expect(() =>
seeder.readCollectionsFromPath('/this/path/surely/doesnt/exist'),
).toThrowError('Error: ENOENT');
).toThrow(
`ENOENT: no such file or directory, scandir '/this/path/surely/doesnt/exist'`,
);
});

it('should throw error when cannot connect to database', async () => {
const config: DeepPartial<SeederConfig> = {
database: 'mongodb://foo:bar@localhost:27017/name',
databaseReconnectTimeout: 1,
databaseReconnectTimeout: 1000,
};

const path = `${IMPORT_DATA_DIR}/index-import`;
const seeder = new Seeder(config);
const collections = seeder.readCollectionsFromPath(path);

await expect(seeder.import(collections)).rejects.toThrowError('Error');
await expect(() => seeder.import(collections)).rejects.toThrow(
`Error connecting to database: MongoServerError: Authentication failed.`,
);
});
});
321 changes: 147 additions & 174 deletions docker-image/test/tester/package-lock.json

Large diffs are not rendered by default.

228 changes: 133 additions & 95 deletions examples/custom-docker-image/sample-data/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b9a1542

Please sign in to comment.