Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

fix: map db response codes to exceptions #27

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/couchdb/couch-db-client.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Logger } from '@nestjs/common';
import {
ForbiddenException,
InternalServerErrorException,
Logger,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { catchError, map, Observable, of, switchMap } from 'rxjs';
import { HttpService } from '@nestjs/axios';
import { AxiosHeaders } from 'axios';
Expand Down Expand Up @@ -60,7 +66,7 @@ export class CouchDbClient {

find<T>(request: { query: object; config: any }): Observable<T> {
return this.httpService
.post<T>(`_find`, request.query, request.config)
.post<T>('_find', request.query, request.config)
.pipe(
map((response) => {
return response.data;
Expand Down Expand Up @@ -125,6 +131,17 @@ export class CouchDbClient {
}

private handleError(err: any) {
this.logger.error(err);
if (err.response?.status === 401) {
throw new UnauthorizedException();
}
if (err.response?.status === 403) {
throw new ForbiddenException();
}
if (err.response?.status === 404) {
throw new NotFoundException();
}

console.error(err);
throw new InternalServerErrorException();
}
}
13 changes: 0 additions & 13 deletions src/report/repository/report-calculation-repository.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,6 @@ export class ReportCalculationRepository {
return calculation.id;
}),
switchMap((calculationId) => {
this.couchDbClient.getDatabaseDocument<FetchReportCalculationsResponse>(
{
documentId: `_all_docs`,
config: {
params: {
include_docs: true,
start_key: '"' + calculationId + '"',
end_key: '"ReportCalculation' + '\ufff0"', // ufff0 -> high value unicode character
},
},
},
);

return this.couchDbClient
.find<FindResponse<ReportData>>({
query: {
Expand Down
Loading