Skip to content

Commit

Permalink
Remove type casting in website routes
Browse files Browse the repository at this point in the history
Issue: ARSN-441
  • Loading branch information
williamlardier committed Nov 26, 2024
1 parent bb1dd25 commit fc049d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/s3routes/routes/routeWebsite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RequestLogger } from 'werelogs';

import * as routesUtils from '../routesUtils';
import errors, { ArsenalError } from '../../errors';
import errors from '../../errors';
import * as http from 'http';
import StatsClient from '../../metrics/StatsClient';

Expand Down Expand Up @@ -44,7 +44,7 @@ export default function routerWebsite(
}
// user has their own error page
if (err && dataGetInfo) {
return routesUtils.streamUserErrorPage(err as ArsenalError, dataGetInfo,
return routesUtils.streamUserErrorPage(err, dataGetInfo,
dataRetrievalParams, response, resMetaHeaders, log);
}
// send default error html response
Expand Down Expand Up @@ -77,7 +77,7 @@ export default function routerWebsite(
}
// could redirect on err so check for redirectInfo first
if (err) {
return routesUtils.errorHeaderResponse(err as ArsenalError, response,
return routesUtils.errorHeaderResponse(err, response,
resMetaHeaders, log);
}
return routesUtils.responseContentHeaders(err, {}, resMetaHeaders,
Expand Down
28 changes: 20 additions & 8 deletions lib/s3routes/routesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,17 +680,23 @@ export function responseStreamData(
* @param log - Werelogs logger
*/
export function streamUserErrorPage(
err: ArsenalError,
err: ArsenalError | Error,
dataLocations: { size: string | number }[],
retrieveDataParams: any,
response: http.ServerResponse,
corsHeaders: { [key: string]: string },
log: RequestLogger,
) {
let error: ArsenalError;
if (err instanceof ArsenalError) {
error = err;
} else {
error = errors.InternalError.customizeDescription(err.message);
}
setCommonResponseHeaders(corsHeaders, response, log);
response.setHeader('x-amz-error-code', err.message);
response.setHeader('x-amz-error-message', err.description);
response.writeHead(err.code, { 'Content-type': 'text/html' });
response.setHeader('x-amz-error-code', error.message);
response.setHeader('x-amz-error-message', error.description);
response.writeHead(error.code, { 'Content-type': 'text/html' });
response.on('finish', () => {
log.end().info('responded with streamed content', {
httpCode: response.statusCode,
Expand Down Expand Up @@ -719,7 +725,7 @@ export function errorHtmlResponse(
let error;
if (err instanceof ArsenalError) {
error = err;
} else if (!(err instanceof ArsenalError)) {
} else {
error = errors.InternalError.customizeDescription(err.message);
}

Expand Down Expand Up @@ -782,17 +788,23 @@ export function errorHtmlResponse(
* @param log - Werelogs logger
*/
export function errorHeaderResponse(
err: ArsenalError,
err: ArsenalError | Error,
response: http.ServerResponse,
corsHeaders: { [key: string]: string },
log: RequestLogger,
) {
let error: ArsenalError;
if (err instanceof ArsenalError) {
error = err;
} else {
error = errors.InternalError.customizeDescription(err.message);
}
log.trace('sending error header response',
{ err });
setCommonResponseHeaders(corsHeaders, response, log);
response.setHeader('x-amz-error-code', err.message);
response.setHeader('x-amz-error-message', err.description);
response.writeHead(err.code);
response.setHeader('x-amz-error-message', error.description);
response.writeHead(error.code);
return response.end(() => {
log.end().info('responded with error headers', {
httpCode: response.statusCode,
Expand Down

0 comments on commit fc049d0

Please sign in to comment.