Skip to content

Commit

Permalink
Merge pull request #52 from snyk-tech-services/develop
Browse files Browse the repository at this point in the history
Release: reduce open file handles
  • Loading branch information
lili2311 authored Feb 1, 2022
2 parents 7aa8ba5 + 39601ba commit 55d1ede
Show file tree
Hide file tree
Showing 11 changed files with 561 additions and 482 deletions.
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

0 comments on commit 55d1ede

Please sign in to comment.