Skip to content

Commit

Permalink
Fix lint warnings (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
huntharo authored Apr 25, 2024
1 parent 5d64b23 commit 19f4621
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 70 deletions.
3 changes: 0 additions & 3 deletions packages/demo-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { readFileSync } from 'fs';
const html = readFileSync(`./index.html`, 'utf8');
const file = readFileSync(`./file.html`, 'utf8');

const buildTrigger = '2023-01-24-01';
console.info('Demo-app build trigger', { buildTrigger });

// eslint-disable-next-line @typescript-eslint/require-await
export async function handler(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export async function DeleteVersion(opts: {
RouteId: routeId,
}),
);
} catch (err: any) {
if (err.name === 'AccessDeniedException') {
} catch (err) {
if (err instanceof Error && err.name === 'AccessDeniedException') {
Log.Instance.error('AccessDeniedException removing route from API Gateway', {
error: err,
apiId,
Expand Down Expand Up @@ -110,8 +110,8 @@ export async function DeleteVersion(opts: {
IntegrationId: record.IntegrationID,
}),
);
} catch (error: any) {
if (error.name === 'AccessDeniedException') {
} catch (error) {
if (error instanceof Error && error.name === 'AccessDeniedException') {
Log.Instance.error('AccessDeniedException removing integration from API Gateway', {
error,
apiId,
Expand Down Expand Up @@ -208,17 +208,17 @@ export async function DeleteVersion(opts: {
Qualifier: aliasInfo.FunctionVersion,
}),
);
} catch (error: any) {
if (error.name !== 'ResourceConflictException') {
} catch (error) {
if (!(error instanceof Error && error.name === 'ResourceConflictException')) {
throw error;
}

Log.Instance.info('Version is still in use by another alias, not deleting');
}
}
} catch (error: any) {
} catch (error) {
// It's ok if the Alias or Version is already gone
if (error.name !== 'ResourceNotFoundException') {
if (!(error instanceof Error && error.name === 'ResourceNotFoundException')) {
throw error;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ export async function DeployVersion(opts: {
}
}
}
} catch (error: any) {
if (error.name !== 'ResourceNotFoundException') {
} catch (error) {
if (!(error instanceof Error && error.name === 'ResourceNotFoundException')) {
throw error;
}
}
Expand Down Expand Up @@ -207,8 +207,8 @@ export async function DeployVersion(opts: {
);

integrationId = integration.IntegrationId as string;
} catch (error: any) {
if (error.name === 'AccessDeniedException') {
} catch (error) {
if (error instanceof Error && error.name === 'AccessDeniedException') {
Log.Instance.error('AccessDeniedException adding integration to API Gateway', {
error,
});
Expand Down Expand Up @@ -245,8 +245,8 @@ export async function DeployVersion(opts: {
);
Log.Instance.info('created RouteIDAppVersion', { result });
record.RouteIDAppVersion = `${result.RouteId}`;
} catch (err: any) {
if (err.name === 'AccessDeniedException') {
} catch (err) {
if (err instanceof Error && err.name === 'AccessDeniedException') {
Log.Instance.error('AccessDeniedException adding route to API Gateway', {
error: err,
});
Expand All @@ -255,7 +255,11 @@ export async function DeployVersion(opts: {

// Don't care
Log.Instance.error('Caught unexpected error on app/ver route add');
Log.Instance.error(err);
if (typeof err === 'string' || err instanceof Error) {
Log.Instance.error(err);
} else {
Log.Instance.error('An unknown error occurred');
}
}
}
// Add the route to API Gateway for appName/version/{proxy+}
Expand All @@ -279,8 +283,8 @@ export async function DeployVersion(opts: {
);
Log.Instance.info('created RouteIDAppVersionSplat', { result });
record.RouteIDAppVersionSplat = `${result.RouteId}`;
} catch (err: any) {
if (err.name === 'AccessDeniedException') {
} catch (err) {
if (err instanceof Error && err.name === 'AccessDeniedException') {
Log.Instance.error('AccessDeniedException adding route to API Gateway', {
error: err,
});
Expand All @@ -289,7 +293,11 @@ export async function DeployVersion(opts: {

// Don't care
Log.Instance.error('Caught unexpected error on {proxy+} route add');
Log.Instance.error(err);
if (typeof err === 'string' || err instanceof Error) {
Log.Instance.error(err);
} else {
Log.Instance.error('An unknown error occurred');
}
}
}

Expand Down Expand Up @@ -345,8 +353,8 @@ export async function DeployVersion(opts: {
if (functionUrl.FunctionUrl) {
url = functionUrl.FunctionUrl;
}
} catch (error: any) {
if (error.name !== 'ResourceNotFoundException') {
} catch (error) {
if (!(error instanceof Error && error.name === 'ResourceNotFoundException')) {
throw error;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const theConfig: Writeable<IConfig> = {
rootPathPrefix: 'dev',
requireIAMAuthorization: true,
parentDeployerLambdaARN: '',
edgeToOriginRoleARN: '',
edgeToOriginRoleARN: [],
};
const origConfig = { ...theConfig };
Object.defineProperty(Config, 'instance', {
Expand All @@ -31,10 +31,7 @@ Object.defineProperty(Config, 'instance', {
return theConfig;
}),
});
import * as apigwy from '@aws-sdk/client-apigatewayv2';
import * as dynamodb from '@aws-sdk/client-dynamodb';
import * as lambda from '@aws-sdk/client-lambda';
import * as s3 from '@aws-sdk/client-s3';

import type {
ICreateApplicationRequest,
Expand All @@ -43,7 +40,6 @@ import type {
} from '@pwrdrvr/microapps-deployer-lib';
import { DBManager, Version } from '@pwrdrvr/microapps-datalib';
import type * as lambdaTypes from 'aws-lambda';
import { mockClient, AwsClientStub } from 'aws-sdk-client-mock';
import sinon from 'sinon';
import { handler, overrideDBManager } from '../../index';

Expand All @@ -53,9 +49,7 @@ let dbManager: DBManager;
const TEST_TABLE_NAME = 'microapps';

describe('GetVersion', () => {
const config = Config.instance;
let sandbox: sinon.SinonSandbox;
const pathPrefix = `${config.rootPathPrefix}/`;

beforeAll(() => {
dynamoClient = new dynamodb.DynamoDBClient({
Expand Down
29 changes: 18 additions & 11 deletions packages/microapps-deployer/src/controllers/version/LambdaAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,20 @@ export async function LambdaAlias(opts: {
errorMessage: `Lambda version not found: ${lambdaVersion}, for lambda: ${lambdaARN}`,
};
}
} catch (error: any) {
Log.Instance.error('Error getting Lambda version', error);

if (error.name === 'ResourceNotFoundException') {
} catch (error) {
if (error instanceof Error && error.name === 'ResourceNotFoundException') {
return { statusCode: 404, errorMessage: error.message };
} else {
return { statusCode: 500, errorMessage: error.message };
if (error instanceof Error) {
Log.Instance.error('Error getting Lambda version', error);
return { statusCode: 500, errorMessage: error.message };
} else if (typeof error === 'string') {
Log.Instance.error(`Error getting Lambda version: ${error}`);
return { statusCode: 500, errorMessage: error };
} else {
Log.Instance.error('Error getting Lambda version: unknown error');
return { statusCode: 500, errorMessage: 'An unknown error occurred' };
}
}
}

Expand Down Expand Up @@ -230,8 +237,8 @@ async function AddOrUpdateFunctionUrl({
if (functionUrl.FunctionUrl) {
url = functionUrl.FunctionUrl;
}
} catch (error: any) {
if (error.name !== 'ResourceNotFoundException') {
} catch (error) {
if (!(error instanceof Error && error.name === 'ResourceNotFoundException')) {
throw error;
}
}
Expand Down Expand Up @@ -321,8 +328,8 @@ async function CreateOrUpdateLambdaAlias(opts: {
lambdaAliasARN: resultLambdaAlias.AliasArn,
actionTaken: 'updated',
};
} catch (error: any) {
if (error.name !== 'ResourceNotFoundException') {
} catch (error) {
if (!(error instanceof Error && error.name == 'ResourceNotFoundException')) {
throw error;
}

Expand Down Expand Up @@ -453,8 +460,8 @@ async function AddCrossAccountPermissionsToAlias({
if (existingPolicy.Policy) {
policyDoc = JSON.parse(existingPolicy.Policy) as IPolicyDocument;
}
} catch (error: any) {
if (error.name !== 'ResourceNotFoundException') {
} catch (error) {
if (!(error instanceof Error && error.name === 'ResourceNotFoundException')) {
throw error;
}

Expand Down
11 changes: 6 additions & 5 deletions packages/microapps-deployer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ let dynamoClient = new DynamoDBClient({
maxAttempts: 8,
});

const buildTrigger = '2023-01-31-01';
Log.Instance.info('Deployer build trigger', { buildTrigger });

export function overrideDBManager(opts: {
dbManager: DBManager;
dynamoClient: DynamoDBClient;
Expand Down Expand Up @@ -167,9 +164,13 @@ export async function handler(
default:
return { statusCode: 400 };
}
} catch (err: any) {
} catch (err) {
Log.Instance.error('Caught unexpected exception in handler');
Log.Instance.error(err);
if (typeof err === 'string' || err instanceof Error) {
Log.Instance.error(err);
} else {
Log.Instance.error('An unknown error occurred');
}
return { statusCode: 500 };
}
}
14 changes: 1 addition & 13 deletions packages/microapps-edge-to-origin/src/index.route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,6 @@ describe('edge-to-origin - routing - without prefix', () => {
SemVer2: '1.2.1-beta.2',
Locales: [],
RawPath: '/_next/data/1.2.1-beta.2/batdirectnobasenextdata/route.json',
SuffixPath: 'batdirectnobasenextdata/route.json',
},
{
AppName: 'BatDirectNoBaseNextDataRootRoute',
Expand All @@ -502,7 +501,6 @@ describe('edge-to-origin - routing - without prefix', () => {
SemVer2: '1.2.1-beta.4',
Locales: [],
RawPath: '/_next/data/1.2.1-beta.4/batdirectnobasenextdatarootroute.json',
SuffixPath: 'batdirectnobasenextdatarootroute.json',
},
{
AppName: 'BatDirectNoBaseNextDataRootRouteLocale',
Expand All @@ -512,22 +510,12 @@ describe('edge-to-origin - routing - without prefix', () => {
SemVer2: '1.2.1-beta.4',
Locales: ['en', 'sv'],
RawPath: '/_next/data/1.2.1-beta.4/sv/batdirectnobasenextdatarootroutelocale.json',
SuffixPath: 'batdirectnobasenextdatarootroutelocale.json',
},
];

it.each(testCases)(
'should route `direct` /_next/data/[$SemVer2]/[$AppName] request to [$AppName] when it exists but $SemVer1 is the default',
async ({
AppName,
LambdaURL1,
SemVer1,
LambdaURL2,
SemVer2,
RawPath,
Locales,
SuffixPath,
}) => {
async ({ AppName, LambdaURL1, SemVer1, LambdaURL2, SemVer2, RawPath, Locales }) => {
theConfig.replaceHostHeader = true;
theConfig.locales = Locales;

Expand Down
3 changes: 0 additions & 3 deletions packages/microapps-edge-to-origin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ const appFrame = loadAppFrame({ basePath: __dirname });

log.info('loaded config', { config });

const buildTrigger = '2023-02-01-01';
log.info('Edge-to-origin build trigger', { buildTrigger });

let dbManager: DBManager | undefined;
let dynamoClient = new DynamoDBClient({
maxAttempts: 8,
Expand Down
2 changes: 1 addition & 1 deletion packages/microapps-router-lib/src/get-app-info.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('GetAppInfo', () => {
expected: '[root]',
},
];

// eslint-disable-next-line @typescript-eslint/no-unused-vars
it.each(testCases)('$caseName', async ({ caseName, appName, mockRules, expected }) => {
getAppVersionCacheSpy.mockImplementation(() => {
return {
Expand Down
25 changes: 20 additions & 5 deletions packages/microapps-router-lib/src/get-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,26 @@ export async function GetRoute(event: IGetRouteEvent): Promise<IGetRouteResult>
statusCode: 599,
errorMessage: `Router - Could not route: ${event.rawPath}, no matching route`,
};
} catch (error: any) {
} catch (error) {
log.error('unexpected exception - returning 599', { statusCode: 599, error });
return {
statusCode: 599,
errorMessage: `Router - Could not route: ${event.rawPath}, ${error.message}`,
};
if (error instanceof Error) {
log.error('unexpected exception - returning 599', { statusCode: 599, error });
return {
statusCode: 599,
errorMessage: `Router - Could not route: ${event.rawPath}, ${error.message}`,
};
} else if (typeof error === 'string') {
log.error('unexpected exception - returning 599', { statusCode: 599, error });
return {
statusCode: 599,
errorMessage: `Router - Could not route: ${event.rawPath}, ${error}`,
};
} else {
log.error('unexpected exception - returning 599', { statusCode: 599, error });
return {
statusCode: 599,
errorMessage: `Router - Could not route: ${event.rawPath}, unknown error`,
};
}
}
}
10 changes: 8 additions & 2 deletions packages/microapps-router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,18 @@ export async function handler(
} else {
throw new Error('Unmatched route');
}
} catch (error: any) {
} catch (error) {
log.error('unexpected exception - returning 599', { statusCode: 599, error });
response.statusCode = 599;
response.headers = {};
response.headers['Content-Type'] = 'text/plain';
response.body = `Router - Could not route: ${event.rawPath}, ${error.message}`;
if (error instanceof Error) {
response.body = `Router - Could not route: ${event.rawPath}, ${error.message}`;
} else if (typeof error === 'string') {
response.body = `Router - Could not route: ${event.rawPath}, ${error}`;
} else {
response.body = `Router - Could not route: ${event.rawPath}, unknown error`;
}
}

return response;
Expand Down

0 comments on commit 19f4621

Please sign in to comment.