Skip to content

Commit

Permalink
feat(init): add standard errors and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed Sep 16, 2023
1 parent 70c535a commit 4b8a95f
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 62 deletions.
7 changes: 4 additions & 3 deletions jest.unit.env.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { stage, Stage } from './src/utils/environment';

/**
* sanity check that unit tests are only run in 'test' environment
* - if they are run in prod environment, we could load a bunch of junk data into our prod databases, which would be no bueno
*/
if (stage !== Stage.TEST && process.env.I_KNOW_WHAT_IM_DOING !== 'true')
if (
(process.env.NODE_ENV !== 'test' || process.env.STAGE) &&
process.env.I_KNOW_WHAT_IM_DOING !== 'true'
)
throw new Error(`unit-test is not targeting stage 'test'`);
134 changes: 75 additions & 59 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,8 @@
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"dependencies": {
"type-fns": "0.9.0"
}
}
10 changes: 10 additions & 0 deletions src/BadRequestError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BadRequestError } from './BadRequestError';

describe('BadRequestError', () => {
it('should produce a helpful, observable error message', () => {
const error = new BadRequestError('no tires on the vehicle', {
tires: [],
});
expect(error).toMatchSnapshot();
});
});
17 changes: 17 additions & 0 deletions src/BadRequestError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { HelpfulError } from './HelpfulError';

/**
* BadRequestError errors are used to explicitly declare that your logic has successfully rejected a request
*
* Named after HTTPStatusCode_400
* - > The server cannot or will not process the request due to an apparent caller error
* - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400
*
* Commonly used to return an error to the caller while marking the execution as successful
* - e.g., the [simple-lambda-handlers](https://github.com/ehmpathy/simple-lambda-handlers) library returns an error to the caller (to notify them of the rejection) while marking the lambda invocation as successful (to avoid cloudwatch metric errors and automated retries)
*/
export class BadRequestError extends HelpfulError {
constructor(message: string, metadata?: Record<string, any>) {
super(['BadRequestError: ', message].join(''), metadata);
}
}
10 changes: 10 additions & 0 deletions src/HelpfulError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { HelpfulError } from './HelpfulError';

describe('HelpfulError', () => {
it('should produce a helpful, observable error message', () => {
const error = new HelpfulError('the dogs were let out', {
who: 'your mom',
});
expect(error).toMatchSnapshot();
});
});
Loading

0 comments on commit 4b8a95f

Please sign in to comment.