Files
twenty/server/src/utils/assert.ts
gitstart-twenty 00a3c8ca2b Change to using arrow functions (#1603)
* Change to using arrow functions

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Matheus <matheus_benini@hotmail.com>

* Add lint rule

---------

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Matheus <matheus_benini@hotmail.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
2023-09-15 18:41:10 -07:00

28 lines
600 B
TypeScript

import { HttpException } from '@nestjs/common';
type Assert = (
condition: unknown,
message?: string,
ErrorType?: new (message?: string) => HttpException,
) => asserts condition;
/**
* assert condition and throws a HttpException
*/
export const assert: Assert = (condition, message, ErrorType) => {
if (!condition) {
if (ErrorType) {
if (message) {
throw new ErrorType(message);
}
throw new ErrorType();
}
throw new Error(message);
}
};
export const assertNotNull = <T>(item: T): item is NonNullable<T> =>
item !== null && item !== undefined;