Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw on non-ok server responses #155

Merged
merged 1 commit into from
Aug 21, 2023
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- This package no longer supports Node v14
## Changed
- This package no longer supports Node v14.
- The `deleteFolder` and `deleteArchiveRecord` SDK methods now return void (was boolean).
- All SDK methods now throw if the server responds with a non-OK status.

## Added
- The `HttpResponseError` type now exists.

## [0.6.0] - 2023-05-18
## Changed
Expand Down
17 changes: 7 additions & 10 deletions src/api/__tests__/deleteFolderVo.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
import nock from 'nock';
import { deleteFolderVo } from '..';
import { HttpResponseError } from '../../errors';

describe('deleteFolderVo', () => {
it('should return true when a folder is deleted', async () => {
it('should not throw when a folder is deleted', async () => {
nock('https://permanent.local')
.delete(
'/api/folder/delete?folderId=1',
)
.reply(200);

const result = await deleteFolderVo(
await expect(deleteFolderVo(
{
bearerToken: '12345',
baseUrl: 'https://permanent.local/api',
},
1,
);

expect(result).toBe(true);
)).resolves.not.toThrow();
});

it('should return false when a folder is not deleted', async () => {
it('should throw an error when when a 500 status is returned', async () => {
nock('https://permanent.local')
.delete(
'/api/folder/delete?folderId=1',
)
.reply(500);

const result = await deleteFolderVo(
await expect(deleteFolderVo(
{
bearerToken: '12345',
baseUrl: 'https://permanent.local/api',
},
1,
);

expect(result).toBe(false);
)).rejects.toThrow(HttpResponseError);
});
});
17 changes: 7 additions & 10 deletions src/api/__tests__/deleteRecordVo.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
import nock from 'nock';
import { deleteRecordVo } from '..';
import { HttpResponseError } from '../../errors';

describe('deleteArchiveRecord', () => {
it('should return true when a record is deleted', async () => {
it('should not throw when a record is deleted', async () => {
nock('https://permanent.local')
.delete(
'/api/record/delete?recordId=1',
)
.reply(200);

const result = await deleteRecordVo(
await expect(deleteRecordVo(
{
bearerToken: '12345',
baseUrl: 'https://permanent.local/api',
},
1,
);

expect(result).toBe(true);
)).resolves.not.toThrow();
});

it('should return false when a record is not deleted', async () => {
it('should throw an error when when a 500 status is returned', async () => {
nock('https://permanent.local')
.delete(
'/api/record/delete?recordId=1',
)
.reply(500);

const result = await deleteRecordVo(
await expect(deleteRecordVo(
{
bearerToken: '12345',
baseUrl: 'https://permanent.local/api',
},
1,
);

expect(result).toBe(false);
)).rejects.toThrow(HttpResponseError);
});
});
8 changes: 2 additions & 6 deletions src/api/deleteFolderVo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ import type { ClientConfiguration } from '../types';
export const deleteFolderVo = async (
clientConfiguration: ClientConfiguration,
folderId: number,
): Promise<boolean> => {
): Promise<void> => {
const queryParams = new URLSearchParams({
folderId: `${folderId}`,
});
const response = await makePermanentApiCall(
await makePermanentApiCall(
clientConfiguration,
`/folder/delete?${queryParams.toString()}`,
{ method: 'DELETE' },
);
if (response.status === 200) {
return true;
}
return false;
};
8 changes: 2 additions & 6 deletions src/api/deleteRecordVo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ import type { ClientConfiguration } from '../types';
export const deleteRecordVo = async (
clientConfiguration: ClientConfiguration,
recordId: number,
): Promise<boolean> => {
): Promise<void> => {
const queryParams = new URLSearchParams({
recordId: `${recordId}`,
});
const response = await makePermanentApiCall(
await makePermanentApiCall(
clientConfiguration,
`/record/delete?${queryParams.toString()}`,
{ method: 'DELETE' },
);
if (response.status === 200) {
return true;
}
return false;
};
8 changes: 8 additions & 0 deletions src/errors/HttpResponseError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class HttpResponseError extends Error {
public readonly statusCode: number;

public constructor(statusCode: number, message: string) {
super(message);
this.statusCode = statusCode;
}
}
1 change: 1 addition & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './HttpResponseError';
export * from './ValidationError';
17 changes: 7 additions & 10 deletions src/sdk/__tests__/deleteArchiveRecord.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
import nock from 'nock';
import { deleteArchiveRecord } from '..';
import { HttpResponseError } from '../../errors';

describe('deleteArchiveRecord', () => {
it('should return true when a record is deleted', async () => {
it('should not throw on successful deletion', async () => {
nock('https://permanent.local')
.delete(
'/api/record/delete?recordId=1',
)
.reply(200);

const result = await deleteArchiveRecord(
await expect(deleteArchiveRecord(
{
bearerToken: '12345',
baseUrl: 'https://permanent.local/api',
},
1,
);

expect(result).toBe(true);
)).resolves.not.toThrow();
});

it('should return false when a record is not deleted', async () => {
it('should throw an error when receiving a 500 response', async () => {
nock('https://permanent.local')
.delete(
'/api/record/delete?recordId=1',
)
.reply(500);

const result = await deleteArchiveRecord(
await expect(deleteArchiveRecord(
{
bearerToken: '12345',
baseUrl: 'https://permanent.local/api',
},
1,
);

expect(result).toBe(false);
)).rejects.toThrow(HttpResponseError);
});
});
17 changes: 7 additions & 10 deletions src/sdk/__tests__/deleteFolder.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
import nock from 'nock';
import { deleteFolder } from '..';
import { HttpResponseError } from '../../errors/HttpResponseError';

describe('deleteFolder', () => {
it('should return true when a folder is deleted', async () => {
it('should not throw when a folder is deleted', async () => {
nock('https://permanent.local')
.delete(
'/api/folder/delete?folderId=1',
)
.reply(200);

const result = await deleteFolder(
await expect(deleteFolder(
{
bearerToken: '12345',
baseUrl: 'https://permanent.local/api',
},
1,
);

expect(result).toBe(true);
)).resolves.not.toThrow();
});

it('should return false when a folder is not deleted', async () => {
it('should throw an error when receiving a 500 response', async () => {
nock('https://permanent.local')
.delete(
'/api/folder/delete?folderId=1',
)
.reply(500);

const result = await deleteFolder(
await expect(deleteFolder(
{
bearerToken: '12345',
baseUrl: 'https://permanent.local/api',
},
1,
);

expect(result).toBe(false);
)).rejects.toThrow(HttpResponseError);
});
});
2 changes: 1 addition & 1 deletion src/sdk/deleteArchiveRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
export const deleteArchiveRecord = async (
clientConfiguration: ClientConfiguration,
archiveRecordId: number,
): Promise<boolean> => deleteRecordVo(
): Promise<void> => deleteRecordVo(
clientConfiguration,
archiveRecordId,
);
2 changes: 1 addition & 1 deletion src/sdk/deleteFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
export const deleteFolder = async (
clientConfiguration: ClientConfiguration,
folderId: number,
): Promise<boolean> => deleteFolderVo(
): Promise<void> => deleteFolderVo(
clientConfiguration,
folderId,
);
7 changes: 7 additions & 0 deletions src/utils/makePermanentApiCall.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetch from 'node-fetch';
import { HttpResponseError } from '../errors';
import type {
RequestInit,
Response,
Expand Down Expand Up @@ -38,5 +39,11 @@ export const makePermanentApiCall = async (
headers,
},
);
if (!response.ok) {
throw new HttpResponseError(
response.status,
await response.text(),
);
}
return response;
};