Skip to content

Commit

Permalink
Do not make assumption in routes for error instances
Browse files Browse the repository at this point in the history
Issue: ARSN-441
  • Loading branch information
williamlardier committed Nov 26, 2024
1 parent 732cfa8 commit bb1dd25
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/s3routes/routes/routeWebsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function routerWebsite(
// request being redirected
if (redirectInfo) {
if (err && redirectInfo.withError) {
return routesUtils.redirectRequestOnError(err as ArsenalError,
return routesUtils.redirectRequestOnError(err,
'GET', redirectInfo, dataGetInfo, dataRetrievalParams,
response, resMetaHeaders, log)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ export default function routerWebsite(
routesUtils.statsReport500(err, statsClient);
if (redirectInfo) {
if (err && redirectInfo.withError) {
return routesUtils.redirectRequestOnError(err as ArsenalError,
return routesUtils.redirectRequestOnError(err,
'HEAD', redirectInfo, null, dataRetrievalParams,
response, resMetaHeaders, log)
}
Expand Down
17 changes: 12 additions & 5 deletions lib/s3routes/routesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ export function redirectRequest(
* @param log - Werelogs instance
*/
export function redirectRequestOnError(
err: ArsenalError,
err: ArsenalError | Error,
method: 'HEAD' | 'GET',
routingInfo: {
withError: true;
Expand All @@ -918,17 +918,24 @@ export function redirectRequestOnError(
) {
response.setHeader('Location', routingInfo.location);

if (!dataLocations && err.is.Found) {
let error: ArsenalError;
if (err instanceof ArsenalError) {
error = err;
} else {
error = errors.InternalError.customizeDescription(err.message);

Check warning on line 925 in lib/s3routes/routesUtils.ts

View check run for this annotation

Codecov / codecov/patch

lib/s3routes/routesUtils.ts#L924-L925

Added lines #L924 - L925 were not covered by tests
}

if (!dataLocations && error.is.Found) {
if (method === 'HEAD') {
return errorHeaderResponse(err, response, corsHeaders, log);
return errorHeaderResponse(error, response, corsHeaders, log);
}
response.setHeader('x-amz-error-code', err.message);
response.setHeader('x-amz-error-message', err.description);
response.setHeader('x-amz-error-message', error.description);
return errorHtmlResponse(err, false, '', response, corsHeaders, log);
}

// This is reached only for website error document (GET only)
const overrideErrorCode = err.flatten();
const overrideErrorCode = error.flatten();
overrideErrorCode.code = 301;
return streamUserErrorPage(ArsenalError.unflatten(overrideErrorCode)!,
dataLocations || [], retrieveDataParams, response, corsHeaders, log);
Expand Down
7 changes: 2 additions & 5 deletions tests/unit/s3routes/routeDELETE.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('routeDELETE', () => {
);
});

it('should return 204 when objectDelete encounters errors other than NoSuchKey or NoSuchVersion', () => {
it('should return error code when objectDelete encounters non-arsenal errors', () => {
request.objectKey = 'objectKey';
request.query = {};

Expand All @@ -119,10 +119,7 @@ describe('routeDELETE', () => {
routeDELETE(request, response, api, log, statsClient);

expect(routesUtils.responseNoBody).toHaveBeenCalledWith(
null, {}, response, 204, log,
);
expect(routesUtils.statsReport500).toHaveBeenCalledWith(
otherError, statsClient,
otherError, {}, response, undefined, log,
);
});
});

0 comments on commit bb1dd25

Please sign in to comment.