Skip to content

Commit

Permalink
align error handling, strict flag, ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Nov 4, 2024
1 parent e33aab9 commit 67fb56b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
29 changes: 12 additions & 17 deletions api-node/src/common/registry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { AppEntry, Apps } from '../apps/types';
import type { PackageEntry, Packages } from '../packages/types';
import type { AwsLambdaLayers } from '../aws-lambda-layers/types';
import * as semver from 'semver';
import { ValueError } from './valueError';

const SDKS_PATH = path.join('..', 'sdks');
const APPS_PATH = path.join('..', 'apps');
Expand All @@ -37,15 +38,15 @@ export class RegistryService {
if (pkg) {
sdks[link] = pkg;
} else if (strict) {
throw new Error(
throw new ValueError(
`Package ${link}, canonical cannot be resolved: ${canonical}`,
);
}
} catch (error) {
if (strict) {
if (error instanceof ValueError) {
throw error;
}
// If not strict, continue to the next SDK
// IO and other errors are ignored
}
}
} catch (error) {
Expand Down Expand Up @@ -73,22 +74,16 @@ export class RegistryService {
// Packages

getPackages(strict: boolean = false): Packages {
return Array.from(iterPackages()).reduce((acc, canonical) => {
const packageDir = getPackageDirFromCanonical(canonical);
const latestFilePath = path.join(packageDir, 'latest.json');

try {
const packageInfo = JSON.parse(
fs.readFileSync(latestFilePath).toString(),
return Array.from(iterPackages()).reduce((acc, pkgName) => {
const pkg = this.getPackage(pkgName);
if (!pkg && strict) {
throw new ValueError(
`Package does not exist or invalid canonical: ${pkgName}`,
);
return {
...acc,
[packageInfo.canonical]: packageInfo,
};
} catch (e) {
console.error(`Failed to read package: ${canonical}`);
console.error(e);
}

acc[pkg.canonical] = pkg;
return acc;
}, {});
}

Expand Down
5 changes: 4 additions & 1 deletion api-node/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export function isTruthy(value: string): boolean {
export function isTruthy(value: string | undefined): boolean {
if (!value) {
return false;
}
return ['true', '1', 'yes'].includes(value.toLowerCase());
}
6 changes: 6 additions & 0 deletions api-node/src/common/valueError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class ValueError extends Error {
constructor(message: string) {
super(message);
this.name = 'ValueError';
}
}
5 changes: 3 additions & 2 deletions api-node/src/packages/packages.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import {
import { RegistryService } from '../common/registry.service';
import { PackageEntry, Packages, PackageVersions } from './types';
import { ReleaseRegistryCacheInterceptor } from '../common/cache';
import { isTruthy } from 'src/common/utils';

@Controller('packages')
@UseInterceptors(ReleaseRegistryCacheInterceptor)
export class PackagesController {
constructor(private registryService: RegistryService) {}

@Get()
getPackages(@Query('strict') strict: boolean = false): Packages {
return this.registryService.getPackages(strict);
getPackages(@Query('strict') strict?: string): Packages {
return this.registryService.getPackages(isTruthy(strict));
}

@Get('/:package(*)/versions')
Expand Down
4 changes: 2 additions & 2 deletions api-node/src/sdks/sdks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Controller, Get, Param, Query, UseInterceptors } from '@nestjs/common';
import { SdkEntry, Sdks, SdkVersions } from './types';
import { RegistryService } from '../common/registry.service';
import { ReleaseRegistryCacheInterceptor } from '../common/cache';
import { isTruthy } from 'src/common/utils';

@Controller('sdks')
@UseInterceptors(ReleaseRegistryCacheInterceptor)
Expand All @@ -10,8 +11,7 @@ export class SdksController {

@Get()
getSdks(@Query('strict') strict?: string): Sdks {
const isStrict =
strict?.toLowerCase() === 'true' || strict === '1' || strict === 'yes';
const isStrict = isTruthy(strict);
return this.registryService.getSdks(isStrict);
}

Expand Down

0 comments on commit 67fb56b

Please sign in to comment.