Skip to content

Commit

Permalink
eslint replaced lint-config (cont) lint-fix
Browse files Browse the repository at this point in the history
Avec les fix de lint-fix
Signed-off-by: Ricardo Machado <[email protected]>
  • Loading branch information
informachadocom committed Jan 18, 2024
1 parent 0008a3c commit bf4822a
Show file tree
Hide file tree
Showing 22 changed files with 296 additions and 176 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Use Node.js
uses: actions/setup-node@v1
with:
Expand All @@ -21,4 +21,4 @@ jobs:
- run: npm run lint
- run: npm test
env:
CI: true
CI: true
2 changes: 1 addition & 1 deletion run
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ require(`${__dirname}/node_modules/@villedemontreal/scripting/dist/src/run`).run
caporal,
projectRoot: __dirname,
scriptsIndexModule: `./scripts/index`,
testsLocations: [`./dist/src/**/*.test.js`]
testsLocations: [`./dist/src/**/*.test.js`],
});
84 changes: 57 additions & 27 deletions src/apiError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
createNotImplementedError,
createServerError,
createUnauthorizedError,
createUnprocessableEntityError
createUnprocessableEntityError,
} from './apiErrorBuilder';
import { globalConstants } from './config/globalConstants';
import { LogLevel } from './logLevel';
Expand All @@ -29,7 +29,10 @@ describe('API errors', () => {
assert.strictEqual(error.logLevel, LogLevel.ERROR);
assert.strictEqual(error.logMessage, 'myLogMessage');
assert.strictEqual(error.logStackTrace, true);
assert.strictEqual(error.error.code, globalConstants.errors.apiGeneralErrors.codes.GENERIC_ERROR);
assert.strictEqual(
error.error.code,
globalConstants.errors.apiGeneralErrors.codes.GENERIC_ERROR,
);
assert.strictEqual(error.error.message, 'Server error');
});

Expand All @@ -39,7 +42,10 @@ describe('API errors', () => {
assert.strictEqual(error.logLevel, LogLevel.ERROR);
assert.strictEqual(error.logMessage, 'myLogMessage');
assert.strictEqual(error.logStackTrace, true);
assert.strictEqual(error.error.code, globalConstants.errors.apiGeneralErrors.codes.GENERIC_ERROR);
assert.strictEqual(
error.error.code,
globalConstants.errors.apiGeneralErrors.codes.GENERIC_ERROR,
);
assert.strictEqual(error.error.message, 'myPublicMessage');
});

Expand Down Expand Up @@ -69,7 +75,10 @@ describe('API errors', () => {
assert.strictEqual(error.logLevel, LogLevel.DEBUG);
assert.strictEqual(error.logMessage, 'myPublicMessage');
assert.strictEqual(error.logStackTrace, false);
assert.strictEqual(error.error.code, globalConstants.errors.apiGeneralErrors.codes.INVALID_PARAMETER);
assert.strictEqual(
error.error.code,
globalConstants.errors.apiGeneralErrors.codes.INVALID_PARAMETER,
);
assert.strictEqual(error.error.message, 'myPublicMessage');
assert.isTrue(error.error.details === undefined || error.error.details.length === 0);
});
Expand All @@ -78,20 +87,23 @@ describe('API errors', () => {
const details: IApiError[] = [
{
code: 'myDetailCode1',
message: 'myDetailMessage1'
message: 'myDetailMessage1',
},
{
code: 'myDetailCode2',
message: 'myDetailMessage2'
}
message: 'myDetailMessage2',
},
];

const error = createInvalidParameterError('myPublicMessage', details, LogLevel.INFO, true);
assert.strictEqual(error.httpStatus, 400);
assert.strictEqual(error.logLevel, LogLevel.INFO);
assert.strictEqual(error.logMessage, 'myPublicMessage');
assert.strictEqual(error.logStackTrace, true);
assert.strictEqual(error.error.code, globalConstants.errors.apiGeneralErrors.codes.INVALID_PARAMETER);
assert.strictEqual(
error.error.code,
globalConstants.errors.apiGeneralErrors.codes.INVALID_PARAMETER,
);
assert.strictEqual(error.error.message, 'myPublicMessage');
assert.strictEqual(error.error.details.length, 2);
assert.strictEqual(error.error.details[0].code, 'myDetailCode1');
Expand All @@ -106,7 +118,10 @@ describe('API errors', () => {
assert.strictEqual(error.logLevel, LogLevel.DEBUG);
assert.strictEqual(error.logMessage, 'myPublicMessage');
assert.strictEqual(error.logStackTrace, false);
assert.strictEqual(error.error.code, globalConstants.errors.apiGeneralErrors.codes.UNPROCESSABLE_ENTITY);
assert.strictEqual(
error.error.code,
globalConstants.errors.apiGeneralErrors.codes.UNPROCESSABLE_ENTITY,
);
assert.strictEqual(error.error.message, 'myPublicMessage');
assert.isTrue(error.error.details === undefined || error.error.details.length === 0);
});
Expand All @@ -116,20 +131,23 @@ describe('API errors', () => {
{
code: 'myDetailCode1',
message: 'myDetailMessage1',
target: 'aJsonPath'
target: 'aJsonPath',
},
{
code: 'myDetailCode2',
message: 'myDetailMessage2'
}
message: 'myDetailMessage2',
},
];

const error = createUnprocessableEntityError('myPublicMessage', details, LogLevel.INFO, true);
assert.strictEqual(error.httpStatus, 422);
assert.strictEqual(error.logLevel, LogLevel.INFO);
assert.strictEqual(error.logMessage, 'myPublicMessage');
assert.strictEqual(error.logStackTrace, true);
assert.strictEqual(error.error.code, globalConstants.errors.apiGeneralErrors.codes.UNPROCESSABLE_ENTITY);
assert.strictEqual(
error.error.code,
globalConstants.errors.apiGeneralErrors.codes.UNPROCESSABLE_ENTITY,
);
assert.strictEqual(error.error.message, 'myPublicMessage');
assert.strictEqual(error.error.details.length, 2);
assert.strictEqual(error.error.details[0].code, 'myDetailCode1');
Expand All @@ -144,7 +162,10 @@ describe('API errors', () => {
assert.strictEqual(error.logLevel, LogLevel.INFO);
assert.strictEqual(error.logMessage, 'Not implemented');
assert.strictEqual(error.logStackTrace, false);
assert.strictEqual(error.error.code, globalConstants.errors.apiGeneralErrors.codes.NOT_IMPLEMENTED);
assert.strictEqual(
error.error.code,
globalConstants.errors.apiGeneralErrors.codes.NOT_IMPLEMENTED,
);
assert.strictEqual(error.error.message, 'Not implemented yet');
});

Expand All @@ -154,7 +175,10 @@ describe('API errors', () => {
assert.strictEqual(error.logLevel, LogLevel.INFO);
assert.strictEqual(error.logMessage, 'myLogMessage');
assert.strictEqual(error.logStackTrace, false);
assert.strictEqual(error.error.code, globalConstants.errors.apiGeneralErrors.codes.NOT_IMPLEMENTED);
assert.strictEqual(
error.error.code,
globalConstants.errors.apiGeneralErrors.codes.NOT_IMPLEMENTED,
);
assert.strictEqual(error.error.message, 'myPublicMessage');
});

Expand All @@ -164,7 +188,10 @@ describe('API errors', () => {
assert.strictEqual(error.logLevel, LogLevel.INFO);
assert.strictEqual(error.logMessage, 'Unauthorized');
assert.strictEqual(error.logStackTrace, false);
assert.strictEqual(error.error.code, globalConstants.errors.apiGeneralErrors.codes.UNAUTHORIZED);
assert.strictEqual(
error.error.code,
globalConstants.errors.apiGeneralErrors.codes.UNAUTHORIZED,
);
assert.strictEqual(error.error.message, 'Please authenticate yourself first.');
});

Expand All @@ -174,7 +201,10 @@ describe('API errors', () => {
assert.strictEqual(error.logLevel, LogLevel.INFO);
assert.strictEqual(error.logMessage, 'myLogMessage');
assert.strictEqual(error.logStackTrace, false);
assert.strictEqual(error.error.code, globalConstants.errors.apiGeneralErrors.codes.UNAUTHORIZED);
assert.strictEqual(
error.error.code,
globalConstants.errors.apiGeneralErrors.codes.UNAUTHORIZED,
);
assert.strictEqual(error.error.message, 'myPublicMessage');
});

Expand Down Expand Up @@ -207,18 +237,18 @@ describe('API errors', () => {
for (const value of [
{
code: 'some code',
message: 'some message'
message: 'some message',
},
{
code: 'some code',
message: 'some message',
details: []
details: [],
},
{
code: 'some code',
message: 'some message',
target: 'target'
}
target: 'target',
},
] as any) {
assert.isTrue(isApiError(value));
}
Expand All @@ -227,26 +257,26 @@ describe('API errors', () => {
it('is not an ApiError', async () => {
for (const value of [
{
code: 'some code'
code: 'some code',
},
{
message: 'some message'
message: 'some message',
},
{
code: 'some code',
message: 'some message',
nope: 'nope'
nope: 'nope',
},
{
code: 'some code',
message: 'some message',
target: 'target',
details: [],
innererror: {
code: 'some code'
code: 'some code',
},
nope: 123
}
nope: 123,
},
] as any) {
assert.isFalse(isApiError(value));
}
Expand Down
10 changes: 5 additions & 5 deletions src/apiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface IErrorResponse {
/**
* Error Reponse Type Guard
*/
export let isErrorResponse = (obj: any): obj is IErrorResponse => {
export const isErrorResponse = (obj: any): obj is IErrorResponse => {
return obj && utils.isObjectStrict(obj) && 'error' in obj && isApiError(obj.error);
};

Expand Down Expand Up @@ -70,7 +70,7 @@ export interface IApiError {
/**
* Error Type Guard
*/
export let isApiError = (obj: any): obj is IApiError => {
export const isApiError = (obj: any): obj is IApiError => {
if (!obj || !utils.isObjectStrict(obj) || !('code' in obj) || !('message' in obj)) {
return false;
}
Expand Down Expand Up @@ -147,7 +147,7 @@ export interface IApiErrorAndInfo {
/**
* IApiErrorAndInfo Type Guard
*/
export let isApiErrorAndInfo = (obj: any): obj is IApiErrorAndInfo => {
export const isApiErrorAndInfo = (obj: any): obj is IApiErrorAndInfo => {
return (
obj &&
utils.isObjectStrict(obj) &&
Expand All @@ -171,7 +171,7 @@ export class ApiErrorAndInfo extends Error implements IApiErrorAndInfo {
public logMessage: any,
public httpStatus: number,
public logLevel: LogLevel,
public logStackTrace: boolean
public logStackTrace: boolean,
) {
super(
((): string => {
Expand All @@ -180,7 +180,7 @@ export class ApiErrorAndInfo extends Error implements IApiErrorAndInfo {
}

return (logMessage as any).msg || (logMessage as any).message || '';
})()
})(),
);
}
}
33 changes: 21 additions & 12 deletions src/apiErrorBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class ErrorBuilder {
public build = (): ApiErrorAndInfo => {
const error: IApiError = {
code: this._code,
message: this._publicMessage || 'An error occured'
message: this._publicMessage || 'An error occured',
};

if (this._target !== undefined) {
Expand All @@ -109,7 +109,7 @@ export class ErrorBuilder {
this._logMessage,
this._httpStatus || HttpStatusCodes.INTERNAL_SERVER_ERROR,
this._logLevel !== undefined ? this._logLevel : LogLevel.ERROR,
this._logStackTrace !== undefined ? this._logStackTrace : true
this._logStackTrace !== undefined ? this._logStackTrace : true,
);

return errorAndInfo;
Expand All @@ -119,7 +119,7 @@ export class ErrorBuilder {
/**
* ErrorBuilder Type Guard
*/
export let isErrorBuilder = (obj: any): obj is ErrorBuilder => {
export const isErrorBuilder = (obj: any): obj is ErrorBuilder => {
return obj && obj.addDetail !== undefined && obj.build !== undefined;
};

Expand All @@ -136,7 +136,10 @@ export function createError(code: string, logMessage: any): ErrorBuilder {
*
* The log message is mandatory.
*/
export function createServerError(logMessage: any, publicMessage: string = 'Server error'): ApiErrorAndInfo {
export function createServerError(
logMessage: any,
publicMessage = 'Server error',
): ApiErrorAndInfo {
return createError(globalConstants.errors.apiGeneralErrors.codes.GENERIC_ERROR, logMessage)
.httpStatus(HttpStatusCodes.INTERNAL_SERVER_ERROR)
.publicMessage(publicMessage)
Expand All @@ -155,9 +158,9 @@ export function createServerError(logMessage: any, publicMessage: string = 'Serv
*/
export function createNotFoundError(
logMessage: any,
publicMessage: string = 'Not Found',
publicMessage = 'Not Found',
logLevel: LogLevel = LogLevel.DEBUG,
logStackTrace: boolean = false
logStackTrace = false,
): ApiErrorAndInfo {
return createError(globalConstants.errors.apiGeneralErrors.codes.NOT_FOUND, logMessage)
.httpStatus(HttpStatusCodes.NOT_FOUND)
Expand All @@ -179,7 +182,7 @@ export function createInvalidParameterError(
publicMessage: string,
details: IApiError[] = [],
logLevel: LogLevel = LogLevel.DEBUG,
logStackTrace: boolean = false
logStackTrace = false,
): ApiErrorAndInfo {
return createError(globalConstants.errors.apiGeneralErrors.codes.INVALID_PARAMETER, publicMessage)
.httpStatus(HttpStatusCodes.BAD_REQUEST)
Expand All @@ -202,9 +205,12 @@ export function createUnprocessableEntityError(
publicMessage: string,
details: IApiError[] = [],
logLevel: LogLevel = LogLevel.DEBUG,
logStackTrace: boolean = false
logStackTrace = false,
): ApiErrorAndInfo {
return createError(globalConstants.errors.apiGeneralErrors.codes.UNPROCESSABLE_ENTITY, publicMessage)
return createError(
globalConstants.errors.apiGeneralErrors.codes.UNPROCESSABLE_ENTITY,
publicMessage,
)
.httpStatus(HttpStatusCodes.UNPROCESSABLE_ENTITY)
.publicMessage(publicMessage)
.details(details)
Expand All @@ -218,7 +224,7 @@ export function createUnprocessableEntityError(
*/
export function createNotImplementedError(
logMessage: any = 'Not implemented',
publicMessage: string = 'Not implemented yet'
publicMessage = 'Not implemented yet',
) {
return createError(globalConstants.errors.apiGeneralErrors.codes.NOT_IMPLEMENTED, logMessage)
.publicMessage(publicMessage)
Expand All @@ -234,7 +240,7 @@ export function createNotImplementedError(
*/
export function createUnauthorizedError(
logMessage: any = 'Unauthorized',
publicMessage: string = 'Please authenticate yourself first.'
publicMessage = 'Please authenticate yourself first.',
) {
return createError(globalConstants.errors.apiGeneralErrors.codes.UNAUTHORIZED, logMessage)
.publicMessage(publicMessage)
Expand All @@ -249,7 +255,10 @@ export function createUnauthorizedError(
* the user is authenticated but doesn't have sufficient
* rights to access the requested resource.
*/
export function createForbiddenError(logMessage: any = 'Forbidden', publicMessage: string = 'Access denied.') {
export function createForbiddenError(
logMessage: any = 'Forbidden',
publicMessage = 'Access denied.',
) {
return createError(globalConstants.errors.apiGeneralErrors.codes.FORBIDDEN, logMessage)
.publicMessage(publicMessage)
.httpStatus(HttpStatusCodes.FORBIDDEN)
Expand Down
Loading

0 comments on commit bf4822a

Please sign in to comment.