Skip to content

Commit

Permalink
SIMSBIOHUB-239: updated error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisupshall committed Aug 30, 2023
1 parent b8f111e commit 55a9c07
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions api/src/services/critterbase-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios, { AxiosInstance } from 'axios';
import axios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
import { URLSearchParams } from 'url';
import { KeycloakService } from './keycloak-service';

Check warning on line 3 in api/src/services/critterbase-service.ts

View workflow job for this annotation

GitHub Actions / Running Linter and Formatter

Replace `KeycloakService·}·from·'./keycloak-service` with `ApiError,·ApiErrorType·}·from·'../errors/api-error`
import { ApiError, ApiErrorType } from '../errors/api-error';

Check warning on line 4 in api/src/services/critterbase-service.ts

View workflow job for this annotation

GitHub Actions / Running Linter and Formatter

Replace `ApiError,·ApiErrorType·}·from·'../errors/api-error` with `KeycloakService·}·from·'./keycloak-service`

export interface ICritterbaseUser {
username: string;
Expand Down Expand Up @@ -179,8 +180,18 @@ export class CritterbaseService {
headers: {
authorization: `Bearer ${this.getToken()}`,
...this.getUserHeader()
}
},
baseURL: CRITTERBASE_API_HOST
});

this.axiosInstance.interceptors.response.use(
(response: AxiosResponse) => {
return response;
},
(error: AxiosError) => {
return Promise.reject(new ApiError(ApiErrorType.UNKNOWN, `API request failed with status code ${error?.response?.status}`));

Check warning on line 192 in api/src/services/critterbase-service.ts

View workflow job for this annotation

GitHub Actions / Running Linter and Formatter

Replace `new·ApiError(ApiErrorType.UNKNOWN,·`API·request·failed·with·status·code·${error?.response?.status}`)` with `⏎··········new·ApiError(ApiErrorType.UNKNOWN,·`API·request·failed·with·status·code·${error?.response?.status}`)⏎········`
}
);
}

async getToken(): Promise<string> {
Expand All @@ -192,7 +203,7 @@ export class CritterbaseService {
return { user: JSON.stringify(this.user) };
}

async makeGetRequest(endpoint: string, params: QueryParam[]) {
async _makeGetRequest(endpoint: string, params: QueryParam[]) {
const appendParams = new URLSearchParams();
for (const p of params) {
appendParams.append(p.key, p.value);
Expand All @@ -202,14 +213,11 @@ export class CritterbaseService {
return response.data;
}

async makePostPatchRequest<DataType = any>(method: 'post' | 'patch', endpoint: string, data?: DataType) {
const url = `${CRITTERBASE_API_HOST}${endpoint}`;
const response = await this.axiosInstance[method](url, data);
return response.data;
}

async getLookupValues(route: CbRouteKey, params: QueryParam[]) {
return this.makeGetRequest(CbRoutes[route], params);
const query = new URLSearchParams(CbRoutes[route])

Check warning on line 217 in api/src/services/critterbase-service.ts

View workflow job for this annotation

GitHub Actions / Running Linter and Formatter

Insert `;`
params.forEach((param) => query.append(param.key, param.value));

return this.axiosInstance.get(query.toString());
}

async getTaxonMeasurements(taxon_id: string) {
Expand Down Expand Up @@ -243,7 +251,7 @@ export class CritterbaseService {
}

async createCritter(data: IBulkCreate) {
return this.makePostPatchRequest('post', BULK_ENDPOINT, data);
return this.axiosInstance.post(BULK_ENDPOINT, data);
}

async signUp() {
Expand Down

0 comments on commit 55a9c07

Please sign in to comment.