Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Improvements for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki committed Aug 17, 2017
1 parent 9f87a24 commit fc057c1
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions cloud/wfm-rest-api/src/impl/MongoDbRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,21 @@ export class MongoDbRepository<T extends {
if (!this.db) {
return Bluebird.reject(dbError);
}
const cursor = this.collection.find(filter);
return Bluebird.resolve(cursor.count(false, filter))
.then(count => defaultPaginationEngine.buildPageResponse(request, cursor, count)).catch(err => {
const apiError = new ApiError(errorCodes.DB_ERROR, 'Cannot perform mongo query', 500, err);
return Bluebird.reject(apiError);
});
try {
const cursor = this.collection.find(filter);
return Bluebird.resolve(cursor.count(false, filter))
.then(count => defaultPaginationEngine.buildPageResponse(request, cursor, count))
.catch(this.handleError);
} catch (err) {
return this.handleError(err);
}
}

public get(id: string): Bluebird<T> {
if (!this.db) {
return Bluebird.reject(dbError);
}
return Bluebird.resolve(this.collection.findOne({ id })).catch(err => {
const apiError = new ApiError(errorCodes.DB_ERROR, 'Cannot perform mongo query', 500, err);
return Bluebird.reject(apiError);
});
return Bluebird.resolve(this.collection.findOne({ id })).catch(this.handleError);
}

public create(object: T): Bluebird<T> {
Expand All @@ -56,10 +55,8 @@ export class MongoDbRepository<T extends {
return Bluebird.reject(dbError);
}
return Bluebird.resolve(this.collection.insertOne(object))
.then(() => this.get(object.id)).catch(err => {
const apiError = new ApiError(errorCodes.DB_ERROR, 'Cannot perform mongo query', 500, err);
return Bluebird.reject(apiError);
});
.then(() => this.get(object.id))
.catch(this.handleError);
}

public update(object: T): Bluebird<T> {
Expand All @@ -70,21 +67,17 @@ export class MongoDbRepository<T extends {
delete object._id;
const id = object.id;
return Bluebird.resolve(this.collection.updateOne({ id }, object))
.then(() => this.get(object.id)).catch(err => {
const apiError = new ApiError(errorCodes.DB_ERROR, 'Cannot perform mongo query', 500, err);
return Bluebird.reject(apiError);
});
.then(() => this.get(object.id))
.catch(this.handleError);
}

public delete(id: string): Bluebird<boolean> {
if (!this.db) {
return Bluebird.reject(dbError);
}
return Bluebird.resolve(this.collection.deleteOne({ id }))
.then(response => response.result.ok === 1).catch(err => {
const apiError = new ApiError(errorCodes.DB_ERROR, 'Cannot perform mongo query', 500, err);
return Bluebird.reject(apiError);
});
.then(response => response.result.ok === 1)
.catch(this.handleError);
}

/**
Expand All @@ -94,4 +87,9 @@ export class MongoDbRepository<T extends {
this.db = db;
this.collection = this.db.collection<T>(this.collectionName);
}

private handleError(err: Error) {
const apiError = new ApiError(errorCodes.DB_ERROR, 'Cannot perform mongo query', 500, err);
return Bluebird.reject(apiError);
}
}

0 comments on commit fc057c1

Please sign in to comment.