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

Release: reduce open file handles #52

Merged
merged 3 commits into from
Feb 1, 2022
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"description": "Rate controlled and retry enabled request manager to interact with Snyk APIs",
"main": "dist/index.js",
"scripts": {
"format:check": "prettier --check '{''{lib,test}/!(fixtures)/**/*,*}.{js,ts,json,yml}'",
"format": "prettier --write '{''{lib,test}/!(fixtures)/**/*,*}.{js,ts,json,yml}'",
"format:check": "prettier --check '{''{src,test}/!(fixtures)/**/*,*}.{js,ts,json,yml}'",
"format": "prettier --write '{''{src,test}/!(fixtures)/**/*,*}.{js,ts,json,yml}'",
"lint": "npm run format:check && npm run lint:eslint",
"lint:eslint": "eslint --cache '{lib,test}/**/*.ts'",
"lint:eslint": "eslint --cache '{src,test}/**/*.ts'",
"test": "npm run lint && npm run test:unit",
"test:unit": "jest",
"test:coverage": "npm run test:unit -- --coverage",
Expand Down
65 changes: 29 additions & 36 deletions src/lib/customErrors/apiError.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,41 @@


class ApiError extends Error {
data: {}
constructor(message: any){
super(message)
this.name = "ApiError"
this.message = (message || "")
this.data = (message.response?.data || "")
}
data: {};
constructor(message: any) {
super(message);
this.name = 'ApiError';
this.message = message || '';
this.data = message.response?.data || '';
}
}

class ApiAuthenticationError extends Error {
data: {}
constructor(message: any){
super(message)
this.name = "ApiAuthenticationError"
this.message = (message || "")
this.data = (message.response?.data || "")
}
data: {};
constructor(message: any) {
super(message);
this.name = 'ApiAuthenticationError';
this.message = message || '';
this.data = message.response?.data || '';
}
}

class NotFoundError extends Error {
data: {}
constructor(message: any){
super(message)
this.name = "NotFoundError"
this.message = (message || "")
this.data = (message.response?.data || "")
}
data: {};
constructor(message: any) {
super(message);
this.name = 'NotFoundError';
this.message = message || '';
this.data = message.response?.data || '';
}
}

class GenericError extends Error {
data: {}
constructor(message: any){
super(message)
this.name = "Unknown"
this.message = (message || "")
this.data = (message.response?.data || "")
}
data: {};
constructor(message: any) {
super(message);
this.name = 'Unknown';
this.message = message || '';
this.data = message.response?.data || '';
}
}

export {
ApiError,
ApiAuthenticationError,
NotFoundError,
GenericError
}
export { ApiError, ApiAuthenticationError, NotFoundError, GenericError };
16 changes: 6 additions & 10 deletions src/lib/customErrors/inputError.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@


class BadInputError extends Error {
constructor(message: any){
super(message)
this.name = "BadInputError"
this.message = (message || "")
}
constructor(message: any) {
super(message);
this.name = 'BadInputError';
this.message = message || '';
}
}

export {
BadInputError
}
export { BadInputError };
136 changes: 73 additions & 63 deletions src/lib/customErrors/requestManagerErrors.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,89 @@
import * as debugModule from 'debug';

import {ApiError,
ApiAuthenticationError,
NotFoundError,
GenericError
} from './apiError'
const debug = debugModule('snyk')

const requestsManagerErrorOverload = (err: Error, channel: string, requestId: string): Error => {
debug('ERROR:', err);
switch(err?.name){
case 'ApiError':
return new RequestsManagerApiError(err.message, channel, requestId)
case 'ApiAuthenticationError':
return new RequestsManagerApiAuthenticationError(err.message, channel, requestId)
case 'NotFoundError':
return new RequestsManagerNotFoundError(err.message, channel, requestId)
case 'Unknown':
return new RequestsManagerGenericError(err.message, channel, requestId)
default:
} return new RequestsManagerGenericError("Unclassified", channel, requestId)
}
import {
ApiError,
ApiAuthenticationError,
NotFoundError,
GenericError,
} from './apiError';
const debug = debugModule('snyk');

class RequestsManagerApiError extends ApiError {
channel: string
requestId: string
constructor(message: any, channel: string, requestId: string){
super(message)
this.name = "ApiError"
this.channel = channel
this.requestId = requestId
this.message = (message || "")
}
channel: string;
requestId: string;
constructor(message: string, channel: string, requestId: string) {
super(message);
this.name = 'ApiError';
this.channel = channel;
this.requestId = requestId;
this.message = message || '';
}
}

class RequestsManagerApiAuthenticationError extends ApiAuthenticationError {
channel: string
requestId: string
constructor(message: any, channel: string, requestId: string){
super(message)
this.name = "ApiAuthenticationError"
this.channel = channel
this.requestId = requestId
this.message = (message || "")
}
channel: string;
requestId: string;
constructor(message: string, channel: string, requestId: string) {
super(message);
this.name = 'ApiAuthenticationError';
this.channel = channel;
this.requestId = requestId;
this.message = message || '';
}
}

class RequestsManagerNotFoundError extends NotFoundError {
channel: string
requestId: string
constructor(message: any, channel: string, requestId: string){
super(message)
this.name = "NotFoundError"
this.channel = channel
this.requestId = requestId
this.message = (message || "")
}
channel: string;
requestId: string;
constructor(message: string, channel: string, requestId: string) {
super(message);
this.name = 'NotFoundError';
this.channel = channel;
this.requestId = requestId;
this.message = message || '';
}
}

class RequestsManagerGenericError extends GenericError {
channel: string
requestId: string
constructor(message: any, channel: string, requestId: string){
super(message)
this.name = "Unknown"
this.channel = channel
this.requestId = requestId
this.message = (message || "")
}
channel: string;
requestId: string;
constructor(message: string, channel: string, requestId: string) {
super(message);
this.name = 'Unknown';
this.channel = channel;
this.requestId = requestId;
this.message = message || '';
}
}

const requestsManagerErrorOverload = (
err: Error,
channel: string,
requestId: string,
): Error => {
debug('ERROR:', err);
switch (err?.name) {
case 'ApiError':
return new RequestsManagerApiError(err.message, channel, requestId);
case 'ApiAuthenticationError':
return new RequestsManagerApiAuthenticationError(
err.message,
channel,
requestId,
);
case 'NotFoundError':
return new RequestsManagerNotFoundError(err.message, channel, requestId);
case 'Unknown':
return new RequestsManagerGenericError(err.message, channel, requestId);
default:
}
return new RequestsManagerGenericError('Unclassified', channel, requestId);
};

export {
RequestsManagerApiError,
RequestsManagerApiAuthenticationError,
RequestsManagerNotFoundError,
RequestsManagerGenericError,
requestsManagerErrorOverload
}
RequestsManagerApiError,
RequestsManagerApiAuthenticationError,
RequestsManagerNotFoundError,
RequestsManagerGenericError,
requestsManagerErrorOverload,
};
59 changes: 29 additions & 30 deletions src/lib/error.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import * as chalk from 'chalk'
import * as chalk from 'chalk';
import debugModule = require('debug');

const handleError = (error: Error): void => {
const debug = debugModule('snyk');
if (!process.env.DEBUG) {
console.log(chalk.hex('#316fcc')('hint: Check debug mode -d'));
}
switch (error.name) {
case 'ApiError':
console.log('Uh oh, seems like we messed something up?');
debug(error);
break;
case 'ApiAuthenticationError':
console.log('Hum, looks like we have a wrong token?');
debug(error);
break;
case 'NotFoundError':
console.log("Couldn't find find this resource");
debug(error);
break;
case 'BadInputError':
console.log('Bad input. Please check the --help');
debug(error);
break;
default:
//console.log("Unknown error")
debug(error);
}
};

const handleError = (error: Error) => {
const debug = debugModule('snyk')
if(!process.env.DEBUG) {
console.log(chalk.hex("#316fcc")("hint: Check debug mode -d"))
}
switch(error.name){
case 'ApiError':
console.log("Uh oh, seems like we messed something up?")
debug(error)
break;
case 'ApiAuthenticationError':
console.log("Hum, looks like we have a wrong token?")
debug(error)
break;
case 'NotFoundError':
console.log("Couldn't find find this resource")
debug(error)
break;
case 'BadInputError':
console.log("Bad input. Please check the --help")
debug(error)
break;
default:
//console.log("Unknown error")
debug(error)
}
}

export default handleError
export default handleError;
Loading