Skip to content

Better Error Handling #42

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

Merged
merged 5 commits into from
Jun 20, 2023
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
65 changes: 49 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/cors": "^2.8.13",
"@types/express": "^4.17.16",
"@types/http-errors": "^2.0.1",
"@types/http-status-codes": "^1.2.0",
"@types/jest": "^29.4.0",
"@types/js-yaml": "^4.0.5",
"@types/lodash.camelcase": "^4.3.7",
Expand Down Expand Up @@ -69,8 +70,8 @@
"@aws-sdk/client-api-gateway": "^3.267.0",
"@aws-sdk/client-s3": "^3.288.0",
"@aws-sdk/credential-providers": "^3.282.0",
"@tinystacks/ops-core": "^0.3.2",
"@tinystacks/ops-model": "^0.4.0",
"@tinystacks/ops-core": "^0.4.0",
"@tinystacks/ops-model": "^0.5.0",
"@types/react": "^18.0.28",
"body-parser": "^1.20.1",
"cached": "^6.1.0",
Expand All @@ -80,6 +81,7 @@
"express": "^4.18.2",
"express-openapi": "^12.1.0",
"http-errors": "^2.0.0",
"http-status-codes": "^2.2.0",
"js-yaml": "^4.1.0",
"json-refs": "^3.0.15",
"lodash.camelcase": "^4.3.0",
Expand Down
12 changes: 6 additions & 6 deletions src/clients/console-client/local.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import yaml from 'js-yaml';
import isNil from 'lodash.isnil';
import { ConsoleParser } from '@tinystacks/ops-core';
import { Console as ConsoleType, YamlConsole } from '@tinystacks/ops-model';
import { Config, Console as ConsoleType, YamlConsole } from '@tinystacks/ops-model';
import HttpError from 'http-errors';
import {
writeFileSync
Expand All @@ -11,6 +10,7 @@ import {
} from 'path';
import FsUtils from '../../utils/fs-utils.js';
import IConsoleClient from './i-console-client.js';
import Yaml from '../../utils/yaml.js';

class LocalConsoleClient implements IConsoleClient {
async getConsole (_consoleName?: string): Promise<ConsoleParser> {
Expand All @@ -20,10 +20,10 @@ class LocalConsoleClient implements IConsoleClient {
// console.debug('configFilePath: ', configFilePath);
const configFile = FsUtils.tryToReadFile(configFilePath);
if (!configFile) throw HttpError.NotFound(`Cannot fetch console! Config file ${configPath} not found!`);
const configJson = (yaml.load(configFile.toString()) as any)?.Console as YamlConsole;
const configJson = Yaml.parseAs<Config>(configFile.toString());
// console.debug('configJson: ', JSON.stringify(configJson));
if (!isNil(configJson)) {
const consoleType: ConsoleType = ConsoleParser.parse(configJson);
if (!isNil(configJson?.Console)) {
const consoleType: ConsoleType = ConsoleParser.parse(configJson?.Console as YamlConsole);
return ConsoleParser.fromJson(consoleType);
}
throw HttpError.InternalServerError('Cannot fetch console! The contents of the config file was empty or invalid!');
Expand All @@ -41,7 +41,7 @@ class LocalConsoleClient implements IConsoleClient {
const previousConsole = await this.getConsole(consoleName);
console.providers = previousConsole.providers;
const yamlConsole = await console.toYaml();
const consoleYml = yaml.dump({ Console: yamlConsole });
const consoleYml = Yaml.stringify({ Console: yamlConsole });
const configPath = process.env.CONFIG_PATH;
if (isNil(configPath)) throw HttpError.InternalServerError(`Cannot save console ${console.name}! No value was found for CONFIG_PATH!`);
try {
Expand Down
12 changes: 6 additions & 6 deletions src/clients/console-client/s3.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import yaml from 'js-yaml';
import isNil from 'lodash.isnil';
import { ConsoleParser } from '@tinystacks/ops-core';
import { Console as ConsoleType, YamlConsole } from '@tinystacks/ops-model';
import { Config, Console as ConsoleType, YamlConsole } from '@tinystacks/ops-model';
import HttpError from 'http-errors';
import {
mkdirSync,
Expand All @@ -12,6 +11,7 @@ import { S3 } from '@aws-sdk/client-s3';
import FsUtils from '../../utils/fs-utils.js';
import IConsoleClient from './i-console-client.js';
import { TMP_DIR } from '../../constants.js';
import Yaml from '../../utils/yaml.js';

type S3Info = {
bucketName: string;
Expand Down Expand Up @@ -177,10 +177,10 @@ class S3ConsoleClient implements IConsoleClient {
*/
const configFile = await this.getConfig();
if (!configFile) throw HttpError.NotFound(`Cannot fetch console! Config file ${configPath} not found!`);
const configJson = (yaml.load(configFile.toString()) as any)?.Console as YamlConsole;
const configJson = Yaml.parseAs<Config>(configFile.toString());
// console.debug('configJson: ', JSON.stringify(configJson));
if (!isNil(configJson)) {
const consoleType: ConsoleType = ConsoleParser.parse(configJson);
if (!isNil(configJson?.Console)) {
const consoleType: ConsoleType = ConsoleParser.parse(configJson?.Console as YamlConsole);
return ConsoleParser.fromJson(consoleType);
}
throw HttpError.InternalServerError('Cannot fetch console! The contents of the config file was empty or invalid!');
Expand All @@ -200,7 +200,7 @@ class S3ConsoleClient implements IConsoleClient {
const previousConsole = await this.getConsole(consoleName);
console.providers = previousConsole.providers;
const yamlConsole = await console.toYaml();
const consoleYml = yaml.dump({ Console: yamlConsole });
const consoleYml = Yaml.stringify({ Console: yamlConsole });
const configPath = process.env.CONFIG_PATH;
if (isNil(configPath)) throw HttpError.InternalServerError(`Cannot save console ${console.name}! No value was found for CONFIG_PATH!`);
try {
Expand Down
10 changes: 5 additions & 5 deletions src/middleware/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import HttpError from 'http-errors';
import { Request, Response, NextFunction } from 'express';
import { TinyStacksError } from '@tinystacks/ops-core';

export default async function errorMiddleware (error: unknown, request: Request, response: Response, next: NextFunction) {
console.error(error);
if (TinyStacksError.isTinyStacksError(error) || HttpError.isHttpError(error)) {
const { status, message } = error as TinyStacksError | HttpError.HttpError;
response.status(status).json({ status, message });
export default async function errorMiddleware (e: unknown, _request: Request, response: Response, next: NextFunction) {
console.error(e);
if (TinyStacksError.isTinyStacksError(e) || HttpError.isHttpError(e)) {
const error = TinyStacksError.fromJson(e as any);
response.status(error.status).json(error);
} else {
const ise = HttpError.InternalServerError('An unexpected error occured! See the API logs for more details.');
response.status(ise.status).json(ise);
Expand Down
45 changes: 45 additions & 0 deletions src/utils/yaml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import jsYaml, { YAMLException } from 'js-yaml';
import { TinyStacksError } from '@tinystacks/ops-core';
import { StatusCodes } from 'http-status-codes';

class Yaml {
static handleError (e: any, message: string): never {
const tsError = TinyStacksError.fromJson({ message, status: StatusCodes.UNPROCESSABLE_ENTITY });
if (e.name === 'YAMLException') {
const error = e as YAMLException;
tsError.cause = error?.reason?.trim();
tsError.context = error?.mark?.snippet?.split('\n')?.map(s => s.trim())?.join('\n');
}
throw tsError;
}

static parseAs<T> (yaml: string): T {
try {
return jsYaml.load(yaml) as T;
} catch (e) {
this.handleError(e, 'Failed to parse yaml!');
}
}

static parse (yaml: string): any {
try {
return jsYaml.load(yaml) as any;
} catch (e) {
this.handleError(e, 'Failed to parse yaml!');
}
}

static stringify (json: any): string {
try {
return jsYaml.dump(json);
} catch (e) {
this.handleError(e, 'Failed to stringify object to yaml!');
}
}
}

export {
Yaml
};

export default Yaml;
24 changes: 20 additions & 4 deletions test/middleware/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ describe('error middleware tests', () => {
expect(mockStatus).toBeCalled();
expect(mockStatus).toBeCalledWith(418);
expect(mockJson).toBeCalled();
expect(mockJson).toBeCalledWith({ status: 418, message: 'mock-error' });
expect(mockJson).toBeCalledWith({
status: 418,
message: 'mock-error',
name: 'TinyStacksError',
type: 'I\'m a teapot',
stack: expect.any(String)
});
expect(mockNext).toBeCalled();
});

Expand All @@ -49,7 +55,7 @@ describe('error middleware tests', () => {
name: 'TinyStacksError',
status: 418,
message: 'mock-error',
type: TinyStacksErrorType.type.VALIDATION
type: 'Validation'
});

await errorMiddleware(mockError, mockRequest, mockResponse, mockNext);
Expand All @@ -59,7 +65,12 @@ describe('error middleware tests', () => {
expect(mockStatus).toBeCalled();
expect(mockStatus).toBeCalledWith(418);
expect(mockJson).toBeCalled();
expect(mockJson).toBeCalledWith({ status: 418, message: 'mock-error' });
expect(mockJson).toBeCalledWith({
status: 418,
message: 'mock-error',
name: 'TinyStacksError',
type: 'Validation'
});
expect(mockNext).toBeCalled();
});

Expand All @@ -78,7 +89,12 @@ describe('error middleware tests', () => {
expect(mockStatus).toBeCalled();
expect(mockStatus).toBeCalledWith(418);
expect(mockJson).toBeCalled();
expect(mockJson).toBeCalledWith({ status: 418, message: 'mock-error' });
expect(mockJson).toBeCalledWith({
status: 418,
message: 'mock-error',
name: 'TinyStacksError',
type: 'Validation'
});
expect(mockNext).toBeCalled();
});

Expand Down