Skip to content

Commit

Permalink
fix: improved error logging when storing jobs in elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
JanssenBrm committed Apr 23, 2024
1 parent 7480d6d commit 9cdebcf
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/jobs/services/database/database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,32 @@ export class DatabaseService {
* @param job - OpenEO job to store
*/
public async saveJobs(job: Job): Promise<Job> {
const response: ApiResponse<Record<string, any>> =
await this.elasticSearch.index({
index: this.JOBS_INDEX,
body: job,
});

if (response.statusCode !== 201) {
this.logger.error(
'Could not save jobs to elasticsearch',
JSON.stringify(response.body),
);
try {
const response: ApiResponse<Record<string, any>> =
await this.elasticSearch.index({
index: this.JOBS_INDEX,
body: job,
});

if (response.statusCode !== 201) {
throw new Error(
`Could not save jobs to elasticsearch: ${JSON.stringify(
response.body,
)}`,
);
}
return job;
} catch (error: any) {
let message = error.toString();
if (error.constructor.name === 'ResponseError') {
message = `${error.meta.body.error.type} - ${error.meta.body.error.reason}`;
}
this.logger.error(`Could not store job in elasticsearch: ${message}`);
throw new HttpException(
'Could not store jobs as general call to database failed',
`Could not store jobs in database: ${message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
return job;
}

/**
Expand All @@ -48,6 +57,7 @@ export class DatabaseService {
* @param limit - The amount of documents to fetch (optional)
* @param idsOnly - Only return the IDs of the document
*/

/* istanbul ignore next */
public async queryJobs(
query: any,
Expand All @@ -74,13 +84,15 @@ export class DatabaseService {
}
return [];
}

/**
* Execute a query on the jobs index.
* @param query - ElasticSearch query to execute
* @param deleted - Flag indicating if the deleted docs should be included (true) or not (false)
* @param limit - The amount of documents to fetch (optional)
* @param idsOnly - Only return the IDs of the document
*/

/* istanbul ignore next */
public async executeJobQuery(
query: any,
Expand Down

0 comments on commit 9cdebcf

Please sign in to comment.