Skip to content

Commit

Permalink
rewrite HTTP client execution to try/catch block, modify types of ret…
Browse files Browse the repository at this point in the history
…urned errors to meet current project's convention
  • Loading branch information
bn-pass committed Nov 22, 2023
1 parent bce7b5a commit 046589f
Showing 1 changed file with 50 additions and 39 deletions.
89 changes: 50 additions & 39 deletions apps/server/src/modules/deletion/client/deletion.client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { firstValueFrom } from 'rxjs';
import { AxiosResponse } from 'axios';
import { Injectable } from '@nestjs/common';
import { Injectable, BadGatewayException } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { ErrorUtils } from '@src/core/error/utils';
import { DeletionRequestInput, DeletionRequestOutput, DeletionClientConfig } from './interface';

@Injectable()
Expand Down Expand Up @@ -30,31 +31,39 @@ export class DeletionClient {
async queueDeletionRequest(input: DeletionRequestInput): Promise<DeletionRequestOutput> {
const request = this.httpService.post(this.postDeletionRequestsEndpoint, input, this.defaultHeaders());

return firstValueFrom(request)
.then((resp: AxiosResponse<DeletionRequestOutput>) => {
// Throw an error if any other status code (other than expected "202 Accepted" is returned).
if (resp.status !== 202) {
throw new Error(`invalid HTTP status code in a response from the server - ${resp.status} instead of 202`);
}

// Throw an error if server didn't return a requestId in a response (and it is
// required as it gives client the reference to the created deletion request).
if (!resp.data.requestId) {
throw new Error('no valid requestId returned from the server');
}

// Throw an error if server didn't return a deletionPlannedAt timestamp so the user
// will not be aware after which date the deletion request's execution will begin.
if (!resp.data.deletionPlannedAt) {
throw new Error('no valid deletionPlannedAt returned from the server');
}

return resp.data;
})
.catch((err: Error) => {
// Throw an error if sending/processing deletion request by the client failed in any way.
throw new Error(`failed to send/process a deletion request: ${err.toString()}`);
});
let resp: AxiosResponse<DeletionRequestOutput>;

try {
resp = (await firstValueFrom(request)) as AxiosResponse<DeletionRequestOutput>;
} catch (err: unknown) {
// Throw an error if sending deletion request has failed.
throw new BadGatewayException('DeletionClient:queueDeletionRequest', ErrorUtils.createHttpExceptionOptions(err));
}

// Throw an error if any other status code (other than expected "202 Accepted" is returned).
if (resp.status !== 202) {
const err = new Error(`invalid HTTP status code in a response from the server - ${resp.status} instead of 202`);

throw new BadGatewayException('DeletionClient:queueDeletionRequest', ErrorUtils.createHttpExceptionOptions(err));
}

// Throw an error if server didn't return a requestId in a response (and it is
// required as it gives client the reference to the created deletion request).
if (!resp.data.requestId) {
const err = new Error('no valid requestId returned from the server');

throw new BadGatewayException('DeletionClient:queueDeletionRequest', ErrorUtils.createHttpExceptionOptions(err));
}

// Throw an error if server didn't return a deletionPlannedAt timestamp so the user
// will not be aware after which date the deletion request's execution will begin.
if (!resp.data.deletionPlannedAt) {
const err = Error('no valid deletionPlannedAt returned from the server');

throw new BadGatewayException('DeletionClient:queueDeletionRequest', ErrorUtils.createHttpExceptionOptions(err));
}

return resp.data;
}

async executeDeletions(limit?: number): Promise<void> {
Expand All @@ -68,19 +77,21 @@ export class DeletionClient {

const request = this.httpService.post(this.postDeletionExecutionsEndpoint, null, requestConfig);

return firstValueFrom(request)
.then((resp: AxiosResponse) => {
// Throw an error if any other status code (other than expected "204 No Content" is returned).
if (resp.status !== 204) {
throw new Error(`invalid HTTP status code in a response from the server - ${resp.status} instead of 204`);
}

return undefined;
})
.catch((err: Error) => {
// Throw an error if sending/processing deletion executions request by the client failed in any way.
throw new Error(`failed to send/process a deletion request: ${err.toString()}`);
});
let resp: AxiosResponse;

try {
resp = await firstValueFrom(request);
} catch (err: unknown) {
// Throw an error if sending deletion request(s) execution trigger has failed.
throw new BadGatewayException('DeletionClient:executeDeletions', ErrorUtils.createHttpExceptionOptions(err));
}

if (resp.status !== 204) {
// Throw an error if any other status code (other than expected "204 No Content" is returned).
const err = new Error(`invalid HTTP status code in a response from the server - ${resp.status} instead of 204`);

throw new BadGatewayException('DeletionClient:executeDeletions', ErrorUtils.createHttpExceptionOptions(err));
}
}

private apiKeyHeader() {
Expand Down

0 comments on commit 046589f

Please sign in to comment.