Skip to content

Commit

Permalink
Merge branch 'next' into conventional-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
antonjoel82 authored Apr 3, 2024
2 parents 82c5ec2 + 0d36d14 commit 5fae86b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libs/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ export * from './services';
export * from './types';
export * from './ui';
export * from './utils';
export * from './services';
export * from './config';
60 changes: 60 additions & 0 deletions libs/shared/src/utils/checkIsResponseError.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { checkIsResponseError } from './checkIsResponseError';
import { IResponseError } from '../types';

describe('checkIsResponseError', () => {
it('should return true for a valid IResponseError object', () => {
const error: IResponseError = {
error: 'Something went wrong',
message: 'An error occurred',
statusCode: 500,
};

const result = checkIsResponseError(error);
expect(result).toBe(true);
});

it('should return false for null', () => {
const result = checkIsResponseError(null);
expect(result).toBe(false);
});

it('should return false for undefined', () => {
const result = checkIsResponseError(undefined);
expect(result).toBe(false);
});

it('should return false for a non-object value', () => {
const result = checkIsResponseError('This is a string');
expect(result).toBe(false);
});

it('should return false if the object is missing the "error" property', () => {
const error = {
message: 'An error occurred',
statusCode: 500,
};

const result = checkIsResponseError(error);
expect(result).toBe(false);
});

it('should return false if the object is missing the "message" property', () => {
const error = {
error: 'Something went wrong',
statusCode: 500,
};

const result = checkIsResponseError(error);
expect(result).toBe(false);
});

it('should return false if the object is missing the "statusCode" property', () => {
const error = {
error: 'Something went wrong',
message: 'An error occurred',
};

const result = checkIsResponseError(error);
expect(result).toBe(false);
});
});
8 changes: 8 additions & 0 deletions libs/shared/src/utils/checkIsResponseError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IResponseError } from '../types';

/**
* Validate (type-guard) that an error response matches our IResponseError interface.
*/
export const checkIsResponseError = (err: unknown): err is IResponseError => {
return !!err && typeof err === 'object' && 'error' in err && 'message' in err && 'statusCode' in err;
};
1 change: 1 addition & 0 deletions libs/shared/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './checkIsResponseError';
export * from './env';

0 comments on commit 5fae86b

Please sign in to comment.