Skip to content

Commit

Permalink
refactor: throw error from es if any
Browse files Browse the repository at this point in the history
  • Loading branch information
KnupMan committed Sep 9, 2019
1 parent 4756b0b commit 31f0eb9
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,19 @@ export async function start(options: StartESOptions): Promise<void> {
debug('ES is running');

await Promise.all(
indexes.map(({name, body}) =>
execSync(
indexes.map(async ({name, body}) => {
const result = execSync(
`curl -X PUT "${esURL}${name}" -H 'Content-Type: application/json' -s -d '${JSON.stringify(
body
)}'`
)
)
);

const error = getESError(result);

if (error) {
throw new Error(`Failed to create index: ${error.reason}`);
}
})
);
debug(`Created ${indexes.length} indexes`);

Expand All @@ -79,7 +85,14 @@ export async function stop(): Promise<void> {
const esVersion = process.env.ES_VERSION;

if (indexes) {
await execSync(`curl -XDELETE ${esURL}${indexes} -s`);
const result = await execSync(`curl -XDELETE ${esURL}${indexes} -s`);

const error = getESError(result);

if (error) {
throw new Error(`Failed to remove index: ${error.reason}`);
}

debug('Removed all indexes');
}

Expand All @@ -98,3 +111,11 @@ async function isExistingFile(filepath: string): Promise<boolean> {
return false;
}
}

interface ESError {
reason: string;
}

function getESError(esResponse: Buffer): ESError | undefined {
return JSON.parse(esResponse.toString()).error;
}

0 comments on commit 31f0eb9

Please sign in to comment.