2021-08-12 15:04:37 +02:00
|
|
|
import express, { Application, RequestHandler } from 'express';
|
2021-04-22 10:07:10 +02:00
|
|
|
import compression from 'compression';
|
|
|
|
import favicon from 'serve-favicon';
|
|
|
|
import cookieParser from 'cookie-parser';
|
|
|
|
import path from 'path';
|
|
|
|
import errorHandler from 'errorhandler';
|
2021-03-11 22:51:58 +01:00
|
|
|
import { responseTimeMetrics } from './middleware/response-time-metrics';
|
2022-08-19 08:09:44 +02:00
|
|
|
import { corsOriginMiddleware } from './middleware/cors-origin-middleware';
|
2021-03-11 22:51:58 +01:00
|
|
|
import rbacMiddleware from './middleware/rbac-middleware';
|
2021-03-29 19:58:11 +02:00
|
|
|
import apiTokenMiddleware from './middleware/api-token-middleware';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashServices } from './types/services';
|
|
|
|
import { IAuthType, IUnleashConfig } from './types/option';
|
|
|
|
import { IUnleashStores } from './types/stores';
|
|
|
|
|
|
|
|
import IndexRouter from './routes';
|
|
|
|
|
|
|
|
import requestLogger from './middleware/request-logger';
|
2021-04-22 10:53:47 +02:00
|
|
|
import demoAuthentication from './middleware/demo-authentication';
|
2021-04-22 10:07:10 +02:00
|
|
|
import ossAuthentication from './middleware/oss-authentication';
|
|
|
|
import noAuthentication from './middleware/no-authentication';
|
|
|
|
import secureHeaders from './middleware/secure-headers';
|
2021-08-12 15:04:37 +02:00
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
import { loadIndexHTML } from './util/load-index-html';
|
2022-08-26 09:25:31 +02:00
|
|
|
import { findPublicFolder } from './util/findPublicFolder';
|
2022-08-26 15:16:29 +02:00
|
|
|
import { conditionalMiddleware } from './middleware/conditional-middleware';
|
2022-09-28 15:53:56 +02:00
|
|
|
import patMiddleware from './middleware/pat-middleware';
|
2022-10-31 10:35:59 +01:00
|
|
|
import { Knex } from 'knex';
|
2022-12-19 08:01:04 +01:00
|
|
|
import maintenanceMiddleware from './middleware/maintenance-middleware';
|
2023-01-26 12:36:45 +01:00
|
|
|
import { unless } from './middleware/unless-middleware';
|
2023-04-12 08:05:34 +02:00
|
|
|
import { catchAllErrorHandler } from './middleware/catch-all-error-handler';
|
Clean up old errors (#3633)
This PR attempts to improve the error handling introduced in #3607.
## About the changes
## **tl;dr:**
- Make `UnleashError` constructor protected
- Make all custom errors inherit from `UnleashError`.
- Add tests to ensure that all special error cases include their
relevant data
- Remove `PasswordMismatchError` and `BadRequestError`. These don't
exist.
- Add a few new error types: `ContentTypeError`, `NotImplementedError`,
`UnauthorizedError`
- Remove the `...rest` parameter from error constructor
- Add an unexported `GenericUnleashError` class
- Move OpenAPI conversion function to `BadDataError` clas
- Remove explicit `Error.captureStackTrace`. This is done automatically.
- Extract `getPropFromString` function and add tests
### **In a more verbose fashion**
The main thing is that all our internal errors now inherit
from`UnleashError`. This allows us to simplify the `UnleashError`
constructor and error handling in general while still giving us the
extra benefits we added to that class. However, it _does_ also mean that
I've had to update **all** existing error classes.
The constructor for `UnleashError` is now protected and all places that
called that constructor directly have been updated. Because the base
error isn't available anymore, I've added three new errors to cover use
cases that we didn't already have covered: `NotImplementedError`,
`UnauthorizedError`, `ContentTypeError`. This is to stay consistent in
how we report errors to the user.
There is also an internal class, `GenericUnleashError` that inherits
from the base error. This class is only used in conversions for cases
where we don't know what the error is. It is not exported.
In making all the errors inherit, I've also removed the `...rest`
parameter from the `UnleashError` constructor. We don't need this
anymore.
Following on from the fixes with missing properties in #3638, I have
added tests for all errors that contain extra data.
Some of the error names that were originally used when creating the list
don't exist in the backend. `BadRequestError` and
`PasswordMismatchError` have been removed.
The `BadDataError` class now contains the conversion code for OpenAPI
validation errors. In doing so, I extracted and tested the
`getPropFromString` function.
### Main files
Due to the nature of the changes, there's a lot of files to look at. So
to make it easier to know where to turn your attention:
The changes in `api-error.ts` contain the main changes: protected
constructor, removal of OpenAPI conversion (moved into `BadDataError`.
`api-error.test.ts` contains tests to make sure that errors work as
expected.
Aside from `get-prop-from-string.ts` and the tests, everything else is
just the required updates to go through with the changes.
## Discussion points
I've gone for inheritance of the Error type over composition. This is in
large part because throwing actual Error instances instead of just
objects is preferable (because they collect stack traces, for instance).
However, it's quite possible that we could solve the same thing in a
more elegant fashion using composition.
## For later / suggestions for further improvements
The `api-error` files still contain a lot of code. I think it might be
beneficial to break each Error into a separate folder that includes the
error, its tests, and its schema (if required). It would help decouple
it a bit.
We don't currently expose the schema anywhere, so it's not available in
the openapi spec. We should look at exposing it too.
Finally, it would be good to go through each individual error message
and update each one to be as helpful as possible.
2023-05-11 11:10:57 +02:00
|
|
|
import NotFoundError from './error/notfound-error';
|
2021-04-22 10:07:10 +02:00
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
export default async function getApp(
|
2021-04-22 10:07:10 +02:00
|
|
|
config: IUnleashConfig,
|
|
|
|
stores: IUnleashStores,
|
|
|
|
services: IUnleashServices,
|
2021-08-12 15:04:37 +02:00
|
|
|
unleashSession?: RequestHandler,
|
2022-10-31 10:35:59 +01:00
|
|
|
db?: Knex,
|
2022-01-06 10:31:00 +01:00
|
|
|
): Promise<Application> {
|
2016-06-18 21:53:18 +02:00
|
|
|
const app = express();
|
2016-11-09 22:31:49 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const baseUriPath = config.server.baseUriPath || '';
|
2023-05-25 13:03:54 +02:00
|
|
|
const publicFolder = config.publicFolder || findPublicFolder();
|
2023-09-29 14:18:21 +02:00
|
|
|
const indexHTML = await loadIndexHTML(config, publicFolder);
|
2021-04-30 15:31:54 +02:00
|
|
|
|
2020-10-02 16:40:42 +02:00
|
|
|
app.set('trust proxy', true);
|
2017-06-29 11:12:44 +02:00
|
|
|
app.disable('x-powered-by');
|
2021-04-22 10:07:10 +02:00
|
|
|
app.set('port', config.server.port);
|
2016-05-01 18:20:10 +02:00
|
|
|
app.locals.baseUriPath = baseUriPath;
|
2021-12-09 21:02:58 +01:00
|
|
|
if (config.server.serverMetrics && config.eventBus) {
|
2023-01-12 11:26:59 +01:00
|
|
|
app.use(
|
|
|
|
responseTimeMetrics(
|
|
|
|
config.eventBus,
|
|
|
|
config.flagResolver,
|
|
|
|
services.instanceStatsService,
|
|
|
|
),
|
|
|
|
);
|
2021-06-07 10:34:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
app.use(requestLogger(config));
|
|
|
|
|
2016-12-28 21:04:26 +01:00
|
|
|
if (typeof config.preHook === 'function') {
|
2022-10-31 10:35:59 +01:00
|
|
|
config.preHook(app, config, services, db);
|
2016-12-28 21:04:26 +01:00
|
|
|
}
|
|
|
|
|
2018-08-22 17:39:09 +02:00
|
|
|
app.use(compression());
|
2016-05-01 22:59:43 +02:00
|
|
|
app.use(cookieParser());
|
2023-01-26 12:36:45 +01:00
|
|
|
|
2023-05-27 18:16:51 +02:00
|
|
|
app.use((req, res, next) => {
|
|
|
|
req.url = req.url.replace(/\/+/g, '/');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2023-01-26 12:36:45 +01:00
|
|
|
app.use(
|
|
|
|
`${baseUriPath}/api/admin/features-batch`,
|
|
|
|
express.json({ strict: false, limit: '500kB' }),
|
|
|
|
);
|
|
|
|
app.use(
|
|
|
|
unless(
|
|
|
|
`${baseUriPath}/api/admin/features-batch`,
|
|
|
|
express.json({ strict: false }),
|
|
|
|
),
|
|
|
|
);
|
2021-08-12 15:04:37 +02:00
|
|
|
if (unleashSession) {
|
|
|
|
app.use(unleashSession);
|
|
|
|
}
|
2020-10-01 21:47:40 +02:00
|
|
|
app.use(secureHeaders(config));
|
2020-09-28 21:54:44 +02:00
|
|
|
app.use(express.urlencoded({ extended: true }));
|
2021-04-22 10:07:10 +02:00
|
|
|
app.use(favicon(path.join(publicFolder, 'favicon.ico')));
|
2022-01-06 10:31:00 +01:00
|
|
|
app.use(baseUriPath, favicon(path.join(publicFolder, 'favicon.ico')));
|
2021-04-30 15:31:54 +02:00
|
|
|
app.use(baseUriPath, express.static(publicFolder, { index: false }));
|
2015-03-10 16:30:56 +01:00
|
|
|
|
2020-12-03 21:09:16 +01:00
|
|
|
if (config.enableOAS) {
|
2021-08-27 12:08:26 +02:00
|
|
|
app.use(`${baseUriPath}/oas`, express.static('docs/api/oas'));
|
2020-12-03 21:09:16 +01:00
|
|
|
}
|
2022-04-25 14:17:59 +02:00
|
|
|
|
|
|
|
if (config.enableOAS && services.openApiService) {
|
|
|
|
services.openApiService.useDocs(app);
|
|
|
|
}
|
2022-08-26 15:16:29 +02:00
|
|
|
// Support CORS preflight requests for the frontend endpoints.
|
|
|
|
// Preflight requests should not have Authorization headers,
|
|
|
|
// so this must be handled before the API token middleware.
|
|
|
|
app.options(
|
2022-10-11 09:20:29 +02:00
|
|
|
`${baseUriPath}/api/frontend*`,
|
2022-08-26 15:16:29 +02:00
|
|
|
conditionalMiddleware(
|
|
|
|
() => config.flagResolver.isEnabled('embedProxy'),
|
2022-11-24 16:14:47 +01:00
|
|
|
corsOriginMiddleware(services, config),
|
2022-08-26 15:16:29 +02:00
|
|
|
),
|
|
|
|
);
|
2022-08-19 08:09:44 +02:00
|
|
|
|
2022-09-28 15:53:56 +02:00
|
|
|
app.use(baseUriPath, patMiddleware(config, services));
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
switch (config.authentication.type) {
|
|
|
|
case IAuthType.OPEN_SOURCE: {
|
|
|
|
app.use(baseUriPath, apiTokenMiddleware(config, services));
|
2023-04-06 11:46:54 +02:00
|
|
|
ossAuthentication(app, config.getLogger, config.server.baseUriPath);
|
2021-04-22 10:07:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IAuthType.ENTERPRISE: {
|
|
|
|
app.use(baseUriPath, apiTokenMiddleware(config, services));
|
|
|
|
config.authentication.customAuthHandler(app, config, services);
|
|
|
|
break;
|
|
|
|
}
|
2021-04-26 21:31:08 +02:00
|
|
|
case IAuthType.HOSTED: {
|
|
|
|
app.use(baseUriPath, apiTokenMiddleware(config, services));
|
|
|
|
config.authentication.customAuthHandler(app, config, services);
|
|
|
|
break;
|
|
|
|
}
|
2021-04-22 10:07:10 +02:00
|
|
|
case IAuthType.DEMO: {
|
2021-05-11 14:15:15 +02:00
|
|
|
app.use(baseUriPath, apiTokenMiddleware(config, services));
|
2021-10-20 13:16:07 +02:00
|
|
|
demoAuthentication(
|
|
|
|
app,
|
|
|
|
config.server.baseUriPath,
|
|
|
|
services,
|
|
|
|
config,
|
|
|
|
);
|
2021-04-22 10:07:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IAuthType.CUSTOM: {
|
|
|
|
app.use(baseUriPath, apiTokenMiddleware(config, services));
|
|
|
|
config.authentication.customAuthHandler(app, config, services);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IAuthType.NONE: {
|
|
|
|
noAuthentication(baseUriPath, app);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2021-10-20 13:16:07 +02:00
|
|
|
app.use(baseUriPath, apiTokenMiddleware(config, services));
|
|
|
|
demoAuthentication(
|
|
|
|
app,
|
|
|
|
config.server.baseUriPath,
|
|
|
|
services,
|
|
|
|
config,
|
|
|
|
);
|
2021-04-22 10:07:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-12-28 21:04:26 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
app.use(
|
|
|
|
baseUriPath,
|
|
|
|
rbacMiddleware(config, stores, services.accessService),
|
|
|
|
);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2022-12-21 12:23:44 +01:00
|
|
|
app.use(
|
|
|
|
`${baseUriPath}/api/admin`,
|
2023-02-27 14:36:56 +01:00
|
|
|
maintenanceMiddleware(config, services.maintenanceService),
|
2022-12-21 12:23:44 +01:00
|
|
|
);
|
2022-12-19 08:01:04 +01:00
|
|
|
|
2021-03-29 19:58:11 +02:00
|
|
|
if (typeof config.preRouterHook === 'function') {
|
2022-10-31 10:35:59 +01:00
|
|
|
config.preRouterHook(app, config, services, stores, db);
|
2021-03-29 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
// Setup API routes
|
2023-02-16 08:08:51 +01:00
|
|
|
app.use(`${baseUriPath}/`, new IndexRouter(config, services, db).router);
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2022-04-25 14:17:59 +02:00
|
|
|
if (services.openApiService) {
|
|
|
|
services.openApiService.useErrorHandler(app);
|
|
|
|
}
|
|
|
|
|
2016-06-18 09:19:57 +02:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2016-11-13 15:31:28 +01:00
|
|
|
app.use(errorHandler());
|
2023-04-12 08:05:34 +02:00
|
|
|
} else {
|
|
|
|
app.use(catchAllErrorHandler(config.getLogger));
|
2016-06-18 09:19:57 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 15:31:54 +02:00
|
|
|
app.get(`${baseUriPath}`, (req, res) => {
|
|
|
|
res.send(indexHTML);
|
|
|
|
});
|
|
|
|
|
feat: unify error responses (#3607)
This PR implements the first version of a suggested unification (and
documentation) of the errors that we return from the API today.
The goal is for this to be the first step towards the error type defined
in this internal [linear
task](https://linear.app/unleash/issue/1-629/define-the-error-type
'Define the new API error type').
## The state of things today
As things stand, we currently have no (or **very** little) documentation
of the errors that are returned from the API. We mention error codes,
but never what the errors may contain.
Second, there is no specified format for errors, so what they return is
arbitrary, and based on ... Who knows? As a result, we have multiple
different errors returned by the API depending on what operation you're
trying to do. What's more, with OpenAPI validation in the mix, it's
absolutely possible for you to get two completely different error
objects for operations to the same endpoint.
Third, the errors we do return are usually pretty vague and don't really
provide any real help to the user. "You don't have the right
permissions". Great. Well what permissions do I need? And how would I
know? "BadDataError". Sick. Why is it bad?
... You get it.
## What we want to achieve
The ultimate goal is for error messages to serve both humans and
machines. When the user provides bad data, we should tell them what
parts of the data are bad and what they can do to fix it. When they
don't have the right permissions, we should tell them what permissions
they need.
Additionally, it would be nice if we could provide an ID for each error
instance, so that you (or an admin) can look through the logs and locate
he incident.
## What's included in **this** PR?
This PR does not aim to implement everything above. It's not intended to
magically fix everything. Its goal is to implement the necessary
**breaking** changes, so that they can be included in v5. Changing error
messages is a slightly grayer area than changing APIs directly, but
changing the format is definitely something I'd consider breaking.
So this PR:
- defines a minimal version of the error type defined in the [API error
definition linear
task](https://linear.app/unleash/issue/1-629/define-the-error-type).
- aims to catch all errors we return today and wrap them in the error
type
- updates tests to match the new expectations.
An important point: because we are cutting v5 very soon and because work
for this wasn't started until last week, the code here isn't necessarily
very polished. But it doesn't need to be. The internals can be as messy
as we want, as long as the API surface is stable.
That said, I'm very open to feedback about design and code completeness,
etc, but this has intentionally been done quickly.
Please also see my inline comments on the changes for more specific
details.
### Proposed follow-ups
As mentioned, this is the first step to implementing the error type. The
public API error type only exposes `id`, `name`, and `message`. This is
barely any more than most of the previous messages, but they are now all
using the same format. Any additional properties, such as `suggestion`,
`help`, `documentationLink` etc can be added as features without
breaking the current format. This is an intentional limitation of this
PR.
Regarding additional properties: there are some error responses that
must contain extra properties. Some of these are documented in the types
of the new error constructor, but not all. This includes `path` and
`type` properties on 401 errors, `details` on validation errors, and
more.
Also, because it was put together quickly, I don't yet know exactly how
we (as developers) would **prefer** to use these new error messages
within the code, so the internal API (the new type, name, etc), is just
a suggestion. This can evolve naturally over time if (based on feedback
and experience) without changing the public API.
## Returning multiple errors
Most of the time when we return errors today, we only return a single
error (even if many things are wrong). AJV, the OpenAPI integration we
use does have a setting that allows it to return all errors in a request
instead of a single one. I suggest we turn that on, but that we do it in
a separate PR (because it updates a number of other snapshots).
When returning errors that point to `details`, the objects in the
`details` now contain a new `description` property. This "deprecates"
the `message` property. Due to our general deprecation policy, this
should be kept around for another full major and can be removed in v6.
```json
{
"name": "BadDataError",
"message": "Something went wrong. Check the `details` property for more information."
"details": [{
"message": "The .params property must be an object. You provided an array.",
"description": "The .params property must be an object. You provided an array.",
}]
}
```
2023-04-25 15:40:46 +02:00
|
|
|
// handle all API 404s
|
|
|
|
app.use(`${baseUriPath}/api`, (req, res) => {
|
Clean up old errors (#3633)
This PR attempts to improve the error handling introduced in #3607.
## About the changes
## **tl;dr:**
- Make `UnleashError` constructor protected
- Make all custom errors inherit from `UnleashError`.
- Add tests to ensure that all special error cases include their
relevant data
- Remove `PasswordMismatchError` and `BadRequestError`. These don't
exist.
- Add a few new error types: `ContentTypeError`, `NotImplementedError`,
`UnauthorizedError`
- Remove the `...rest` parameter from error constructor
- Add an unexported `GenericUnleashError` class
- Move OpenAPI conversion function to `BadDataError` clas
- Remove explicit `Error.captureStackTrace`. This is done automatically.
- Extract `getPropFromString` function and add tests
### **In a more verbose fashion**
The main thing is that all our internal errors now inherit
from`UnleashError`. This allows us to simplify the `UnleashError`
constructor and error handling in general while still giving us the
extra benefits we added to that class. However, it _does_ also mean that
I've had to update **all** existing error classes.
The constructor for `UnleashError` is now protected and all places that
called that constructor directly have been updated. Because the base
error isn't available anymore, I've added three new errors to cover use
cases that we didn't already have covered: `NotImplementedError`,
`UnauthorizedError`, `ContentTypeError`. This is to stay consistent in
how we report errors to the user.
There is also an internal class, `GenericUnleashError` that inherits
from the base error. This class is only used in conversions for cases
where we don't know what the error is. It is not exported.
In making all the errors inherit, I've also removed the `...rest`
parameter from the `UnleashError` constructor. We don't need this
anymore.
Following on from the fixes with missing properties in #3638, I have
added tests for all errors that contain extra data.
Some of the error names that were originally used when creating the list
don't exist in the backend. `BadRequestError` and
`PasswordMismatchError` have been removed.
The `BadDataError` class now contains the conversion code for OpenAPI
validation errors. In doing so, I extracted and tested the
`getPropFromString` function.
### Main files
Due to the nature of the changes, there's a lot of files to look at. So
to make it easier to know where to turn your attention:
The changes in `api-error.ts` contain the main changes: protected
constructor, removal of OpenAPI conversion (moved into `BadDataError`.
`api-error.test.ts` contains tests to make sure that errors work as
expected.
Aside from `get-prop-from-string.ts` and the tests, everything else is
just the required updates to go through with the changes.
## Discussion points
I've gone for inheritance of the Error type over composition. This is in
large part because throwing actual Error instances instead of just
objects is preferable (because they collect stack traces, for instance).
However, it's quite possible that we could solve the same thing in a
more elegant fashion using composition.
## For later / suggestions for further improvements
The `api-error` files still contain a lot of code. I think it might be
beneficial to break each Error into a separate folder that includes the
error, its tests, and its schema (if required). It would help decouple
it a bit.
We don't currently expose the schema anywhere, so it's not available in
the openapi spec. We should look at exposing it too.
Finally, it would be good to go through each individual error message
and update each one to be as helpful as possible.
2023-05-11 11:10:57 +02:00
|
|
|
const error = new NotFoundError(
|
|
|
|
`The path you were looking for (${baseUriPath}/api${req.path}) is not available.`,
|
|
|
|
);
|
feat: unify error responses (#3607)
This PR implements the first version of a suggested unification (and
documentation) of the errors that we return from the API today.
The goal is for this to be the first step towards the error type defined
in this internal [linear
task](https://linear.app/unleash/issue/1-629/define-the-error-type
'Define the new API error type').
## The state of things today
As things stand, we currently have no (or **very** little) documentation
of the errors that are returned from the API. We mention error codes,
but never what the errors may contain.
Second, there is no specified format for errors, so what they return is
arbitrary, and based on ... Who knows? As a result, we have multiple
different errors returned by the API depending on what operation you're
trying to do. What's more, with OpenAPI validation in the mix, it's
absolutely possible for you to get two completely different error
objects for operations to the same endpoint.
Third, the errors we do return are usually pretty vague and don't really
provide any real help to the user. "You don't have the right
permissions". Great. Well what permissions do I need? And how would I
know? "BadDataError". Sick. Why is it bad?
... You get it.
## What we want to achieve
The ultimate goal is for error messages to serve both humans and
machines. When the user provides bad data, we should tell them what
parts of the data are bad and what they can do to fix it. When they
don't have the right permissions, we should tell them what permissions
they need.
Additionally, it would be nice if we could provide an ID for each error
instance, so that you (or an admin) can look through the logs and locate
he incident.
## What's included in **this** PR?
This PR does not aim to implement everything above. It's not intended to
magically fix everything. Its goal is to implement the necessary
**breaking** changes, so that they can be included in v5. Changing error
messages is a slightly grayer area than changing APIs directly, but
changing the format is definitely something I'd consider breaking.
So this PR:
- defines a minimal version of the error type defined in the [API error
definition linear
task](https://linear.app/unleash/issue/1-629/define-the-error-type).
- aims to catch all errors we return today and wrap them in the error
type
- updates tests to match the new expectations.
An important point: because we are cutting v5 very soon and because work
for this wasn't started until last week, the code here isn't necessarily
very polished. But it doesn't need to be. The internals can be as messy
as we want, as long as the API surface is stable.
That said, I'm very open to feedback about design and code completeness,
etc, but this has intentionally been done quickly.
Please also see my inline comments on the changes for more specific
details.
### Proposed follow-ups
As mentioned, this is the first step to implementing the error type. The
public API error type only exposes `id`, `name`, and `message`. This is
barely any more than most of the previous messages, but they are now all
using the same format. Any additional properties, such as `suggestion`,
`help`, `documentationLink` etc can be added as features without
breaking the current format. This is an intentional limitation of this
PR.
Regarding additional properties: there are some error responses that
must contain extra properties. Some of these are documented in the types
of the new error constructor, but not all. This includes `path` and
`type` properties on 401 errors, `details` on validation errors, and
more.
Also, because it was put together quickly, I don't yet know exactly how
we (as developers) would **prefer** to use these new error messages
within the code, so the internal API (the new type, name, etc), is just
a suggestion. This can evolve naturally over time if (based on feedback
and experience) without changing the public API.
## Returning multiple errors
Most of the time when we return errors today, we only return a single
error (even if many things are wrong). AJV, the OpenAPI integration we
use does have a setting that allows it to return all errors in a request
instead of a single one. I suggest we turn that on, but that we do it in
a separate PR (because it updates a number of other snapshots).
When returning errors that point to `details`, the objects in the
`details` now contain a new `description` property. This "deprecates"
the `message` property. Due to our general deprecation policy, this
should be kept around for another full major and can be removed in v6.
```json
{
"name": "BadDataError",
"message": "Something went wrong. Check the `details` property for more information."
"details": [{
"message": "The .params property must be an object. You provided an array.",
"description": "The .params property must be an object. You provided an array.",
}]
}
```
2023-04-25 15:40:46 +02:00
|
|
|
res.status(error.statusCode).send(error);
|
|
|
|
return;
|
|
|
|
});
|
2021-04-30 15:31:54 +02:00
|
|
|
|
feat: unify error responses (#3607)
This PR implements the first version of a suggested unification (and
documentation) of the errors that we return from the API today.
The goal is for this to be the first step towards the error type defined
in this internal [linear
task](https://linear.app/unleash/issue/1-629/define-the-error-type
'Define the new API error type').
## The state of things today
As things stand, we currently have no (or **very** little) documentation
of the errors that are returned from the API. We mention error codes,
but never what the errors may contain.
Second, there is no specified format for errors, so what they return is
arbitrary, and based on ... Who knows? As a result, we have multiple
different errors returned by the API depending on what operation you're
trying to do. What's more, with OpenAPI validation in the mix, it's
absolutely possible for you to get two completely different error
objects for operations to the same endpoint.
Third, the errors we do return are usually pretty vague and don't really
provide any real help to the user. "You don't have the right
permissions". Great. Well what permissions do I need? And how would I
know? "BadDataError". Sick. Why is it bad?
... You get it.
## What we want to achieve
The ultimate goal is for error messages to serve both humans and
machines. When the user provides bad data, we should tell them what
parts of the data are bad and what they can do to fix it. When they
don't have the right permissions, we should tell them what permissions
they need.
Additionally, it would be nice if we could provide an ID for each error
instance, so that you (or an admin) can look through the logs and locate
he incident.
## What's included in **this** PR?
This PR does not aim to implement everything above. It's not intended to
magically fix everything. Its goal is to implement the necessary
**breaking** changes, so that they can be included in v5. Changing error
messages is a slightly grayer area than changing APIs directly, but
changing the format is definitely something I'd consider breaking.
So this PR:
- defines a minimal version of the error type defined in the [API error
definition linear
task](https://linear.app/unleash/issue/1-629/define-the-error-type).
- aims to catch all errors we return today and wrap them in the error
type
- updates tests to match the new expectations.
An important point: because we are cutting v5 very soon and because work
for this wasn't started until last week, the code here isn't necessarily
very polished. But it doesn't need to be. The internals can be as messy
as we want, as long as the API surface is stable.
That said, I'm very open to feedback about design and code completeness,
etc, but this has intentionally been done quickly.
Please also see my inline comments on the changes for more specific
details.
### Proposed follow-ups
As mentioned, this is the first step to implementing the error type. The
public API error type only exposes `id`, `name`, and `message`. This is
barely any more than most of the previous messages, but they are now all
using the same format. Any additional properties, such as `suggestion`,
`help`, `documentationLink` etc can be added as features without
breaking the current format. This is an intentional limitation of this
PR.
Regarding additional properties: there are some error responses that
must contain extra properties. Some of these are documented in the types
of the new error constructor, but not all. This includes `path` and
`type` properties on 401 errors, `details` on validation errors, and
more.
Also, because it was put together quickly, I don't yet know exactly how
we (as developers) would **prefer** to use these new error messages
within the code, so the internal API (the new type, name, etc), is just
a suggestion. This can evolve naturally over time if (based on feedback
and experience) without changing the public API.
## Returning multiple errors
Most of the time when we return errors today, we only return a single
error (even if many things are wrong). AJV, the OpenAPI integration we
use does have a setting that allows it to return all errors in a request
instead of a single one. I suggest we turn that on, but that we do it in
a separate PR (because it updates a number of other snapshots).
When returning errors that point to `details`, the objects in the
`details` now contain a new `description` property. This "deprecates"
the `message` property. Due to our general deprecation policy, this
should be kept around for another full major and can be removed in v6.
```json
{
"name": "BadDataError",
"message": "Something went wrong. Check the `details` property for more information."
"details": [{
"message": "The .params property must be an object. You provided an array.",
"description": "The .params property must be an object. You provided an array.",
}]
}
```
2023-04-25 15:40:46 +02:00
|
|
|
app.get(`${baseUriPath}/*`, (req, res) => {
|
2021-04-30 15:31:54 +02:00
|
|
|
res.send(indexHTML);
|
|
|
|
});
|
2022-04-25 14:17:59 +02:00
|
|
|
|
2016-05-01 18:20:10 +02:00
|
|
|
return app;
|
2021-04-22 10:07:10 +02:00
|
|
|
}
|