Skip to content

Commit

Permalink
Improved error handling in proxy functions and documented the usage o…
Browse files Browse the repository at this point in the history
…f Svelte Kit BODY_SIZE_LIMIT environment variable
  • Loading branch information
zonia3000 committed Feb 18, 2025
1 parent 01b9ccc commit e7fc7de
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ LOG_LEVEL_CONSOLE=warn
FRACTAL_RUNNER_BACKEND=local
#WARNING_BANNER_PATH=/path/to/banner.txt
ENABLE_INTERACTIVE_ATTRIBUTE_FILTERS=false

BODY_SIZE_LIMIT=5000000
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* Used `FRACTAL_BACKEND_RUNNER=local` instead of `local_experimental` (\#713);
* Moved job submission healthcheck page to user menu (\#713);
* Fixed bug in task-groups deletion in admin page (\#713);
* Fixed bug in task order modal drag and drop (\#713);
* Added admin accounting page (\#713);
* Implemented user impersonation (\#713);
* Improved error handling in proxy functions and documented the usage of Svelte Kit `BODY_SIZE_LIMIT` environment variable (\#713);

# 1.15.0

Expand Down
5 changes: 5 additions & 0 deletions __tests__/AlertError.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ describe('AlertError class', () => {
);
expect(error.getSimpleValidationMessage('zarr_url')).eq('error message');
});

it('Extract field error', () => {
const error = new AlertError({ message: 'Payload Too Large' }, 413);
expect(error.getSimpleValidationMessage()).eq('Payload Too Large');
});
});
3 changes: 2 additions & 1 deletion docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ The following environment variables can be used to configure fractal-web.
When running directly using `node` command these extra variables can also be configured:

* `PORT`: specifies the port where Svelte server will run; the default value is 5173;
* `ORIGIN` the URL where the app will be served (e.g. http://localhost:5173, or https://subdomain.example.org).
* `ORIGIN` the URL where the app will be served (e.g. http://localhost:5173, or https://subdomain.example.org);
* `BODY_SIZE_LIMIT`: the maximum request body size accepted in bytes; see [official SvelteKit documentation](https://svelte.dev/docs/kit/adapter-node#Environment-variables-BODY_SIZE_LIMIT).

## Common issues related to environment variables

Expand Down
1 change: 1 addition & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export AUTH_COOKIE_DOMAIN=

export ORIGIN=http://localhost:5173
export PORT=5173
export BODY_SIZE_LIMIT=5000000

export LOG_FILE=fractal-web.log
# default values for logging levels (uncomment if needed)
Expand Down
3 changes: 3 additions & 0 deletions src/lib/common/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ export function extractErrorDetail(errorResponse) {
* @returns {null | { loc: string[], msg: string } | string}
*/
function getSimpleValidationMessage(reason, statusCode) {
if (typeof reason === 'object' && 'message' in reason && typeof reason.message === 'string') {
return reason.message;
}
if (!isValidationError(reason, statusCode)) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/healthcheck/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
{#if inProgress}
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true" />
{/if}
Test
Run test
</button>
</div>
</div>
Expand Down
27 changes: 19 additions & 8 deletions src/routes/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export function createGetProxy(path, forbiddenPaths = []) {
headers: filterHeaders(request.headers)
});
} catch (err) {
logger.debug(err);
throw err;
handleError(err);
}
};
}
Expand All @@ -44,8 +43,7 @@ export function createPostProxy(path, forbiddenPaths = []) {
duplex: 'half'
});
} catch (err) {
logger.debug(err);
throw err;
handleError(err);
}
};
}
Expand All @@ -68,8 +66,7 @@ export function createPatchProxy(path, forbiddenPaths = []) {
duplex: 'half'
});
} catch (err) {
logger.debug(err);
throw err;
handleError(err);
}
};
}
Expand All @@ -89,8 +86,7 @@ export function createDeleteProxy(path, forbiddenPaths = []) {
headers: filterHeaders(request.headers)
});
} catch (err) {
logger.debug(err);
throw err;
handleError(err);
}
};
}
Expand All @@ -107,6 +103,21 @@ function checkForbiddenPaths(path, forbiddenPaths) {
}
}

/**
* @param {unknown} err
*/
function handleError(err) {
if (err instanceof Error && err.cause instanceof Error) {
// Underlying cause might be a Svelte Kit error
error(500, err.cause.message);
} else if (err instanceof Error) {
error(500, err.message);
} else {
logger.debug(err);
throw err;
}
}

/**
* Forward only selected headers, to avoid issues like content length mismatch
* @type {import('fractal-components/types/api').GetHeaders}
Expand Down

0 comments on commit e7fc7de

Please sign in to comment.