Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
fix(errors): add error middleware that handles erorrs thrown, based o…
Browse files Browse the repository at this point in the history
…n their types and returns proper status codes

This type of middleware is particularly useful for this type of problem. We have various instances in which warp throws errors when failing to evaluate a contract state, and we want to parse those properly and return the correct
status code when they occur. Right now we over thrown 503 errors, which suggest to clients that the app failed to proc
ess, when it's more likely they are requesting contracts that have not been mined yet or do not adhere to specs define
d by warp.
  • Loading branch information
dtfiedler committed Sep 20, 2023
1 parent e77d1f9 commit c05dbb9
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 285 deletions.
26 changes: 9 additions & 17 deletions src/api/warp.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
EvalStateResult,
EvaluationManifest,
EvaluationOptions,
Warp,
} from 'warp-contracts';
import { EvaluationManifest, EvaluationOptions, Warp } from 'warp-contracts';
import {
DEFAULT_EVALUATION_OPTIONS,
EVALUATION_TIMEOUT_MS,
allowedContractTypes,
} from '../constants';
import { ContractType } from '../types';
import {
ContractType,
EvaluatedContractState,
EvaluationError,
NotFoundError,
} from '../types';
import * as _ from 'lodash';
import { EvaluationTimeoutError } from '../errors';
import { createHash } from 'crypto';
Expand All @@ -18,16 +18,6 @@ import { Tag } from 'arweave/node/lib/transaction';
import { ReadThroughPromiseCache } from '@ardrive/ardrive-promise-cache';
import winston from 'winston';

export type EvaluatedContractState = EvalStateResult<any> & {
evaluationOptions?: Partial<EvaluationOptions>;
};

export class EvaluationError extends Error {
constructor(message?: string) {
super(message);
}
}

// cache duplicate requests on the same instance within a short period of time
const requestMap: Map<string, Promise<any> | undefined> = new Map();

Check warning on line 22 in src/api/warp.ts

View workflow job for this annotation

GitHub Actions / build (lint:check)

Unexpected any. Specify a different type

Expand Down Expand Up @@ -194,6 +184,8 @@ export async function getContractState({
error.message.includes('Use contract.setEvaluationOptions'))
) {
throw new EvaluationError(error.message);
} else if ((error as any).type.includes('TX_NOT_FOUND')) {

Check warning on line 187 in src/api/warp.ts

View workflow job for this annotation

GitHub Actions / build (lint:check)

Unexpected any. Specify a different type
throw new NotFoundError('Contract not found');
}
throw error;
}
Expand Down
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import {
} from './middleware';
import * as promClient from 'prom-client';
import logger from './logger';
import { errorMiddleware } from './middleware/errors';

const app = new Koa();

// attach middlewares
app.use(loggerMiddleware);
app.use(errorMiddleware);
app.use(arweaveMiddleware);
app.use(warpMiddleware);
app.use(headersMiddleware);
Expand Down
28 changes: 28 additions & 0 deletions src/middleware/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { KoaContext } from '../types';
import { Next } from 'koa';
import { BadRequestError, EvaluationError, NotFoundError } from '../types';

// globally handle errors and return proper status based on their type
export async function errorMiddleware(ctx: KoaContext, next: Next) {
const { logger } = ctx.state;
try {
await next();
} catch (error) {
logger.error('Error processing request.', {
error: error instanceof Error ? error.message : error,
});
if (error instanceof EvaluationError) {
ctx.status = 400;
ctx.body = error.message;
} else if (error instanceof NotFoundError) {
ctx.status = 404;
ctx.body = error.message;
} else if (error instanceof BadRequestError) {
ctx.status = 400;
ctx.body = error.message;
} else {
ctx.status = 503;
ctx.body = 'Internal server error.';
}
}
}
Loading

0 comments on commit c05dbb9

Please sign in to comment.