Skip to content

Commit

Permalink
chore: Remove IErrorSettings. GlobalErrorHandlerMiddleware return a j…
Browse files Browse the repository at this point in the history
…son error object

BREAKING CHANGES:
- If your override GlobalErrorHandlerMiddleware, this change will broke probably your code.
- The response returned is a JSON object instead of raw string. this is a breaking change for your consumers.
- settings.errors.headerName have been removed. There is no replacement. Just implement your own GlobalErrorHandlerMiddleware to change return the expect response to your client.
  • Loading branch information
Romain Lenzotti committed Jul 1, 2020
1 parent 9978b75 commit f0d9d46
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 239 deletions.
6 changes: 0 additions & 6 deletions docs/configuration.md
Expand Up @@ -224,12 +224,6 @@ export class Server {

Logger configuration.

### errors

- type: @@IErrorsSettings@@

Errors configuration. See [Throw Http exceptions](/tutorials/throw-http-exceptions.md) for more details.

### resolvers - External DI <Badge text="v5.39.0+" />

- type: @@IDIResolver@@
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/throw-http-exceptions.md
Expand Up @@ -60,7 +60,7 @@ export class CalendarCtrl {
): any {

if (!user.name) {
throw(new RequiredUserName());
throw new RequiredUserName();
}

return user;
Expand Down
1 change: 0 additions & 1 deletion packages/common/src/config/index.ts
@@ -1,4 +1,3 @@
export * from "./interfaces/IServerMountDirectories";
export * from "./interfaces/ILoggerSettings";
export * from "./interfaces/IErrorSettings";
export * from "./services/PlatformConfiguration";
10 changes: 0 additions & 10 deletions packages/common/src/config/interfaces/IErrorSettings.ts

This file was deleted.

9 changes: 1 addition & 8 deletions packages/common/src/config/interfaces/index.ts
@@ -1,10 +1,8 @@
import {Env} from "@tsed/core";
import {ProviderScope} from "@tsed/di";
import * as Https from "https";
import {IErrorsSettings} from "./IErrorSettings";
import {IConverterSettings} from "./IConverterSettings";
import {ILoggerSettings} from "./ILoggerSettings";
import {IServerMountDirectories} from "./IServerMountDirectories";
import {IConverterSettings} from "./IConverterSettings";

declare global {
namespace TsED {
Expand Down Expand Up @@ -69,10 +67,6 @@ declare global {
* Logger configuration.
*/
logger: Partial<ILoggerSettings>;
/**
* Errors configuration.
*/
errors: Partial<IErrorsSettings>;
/**
* Object to mount all directories under to his endpoints. See more on [Serve Static](/tutorials/serve-static-files.md).
*/
Expand All @@ -81,6 +75,5 @@ declare global {
}
}

export * from "./IErrorSettings";
export * from "./ILoggerSettings";
export * from "./IServerMountDirectories";
Expand Up @@ -24,8 +24,6 @@ describe("GlobalAcceptMimesMiddleware", () => {
const middleware = new GlobalAcceptMimesMiddleware();
middleware.acceptMimes = ["application/json"];

const error = catchError(() => middleware.use(request as any));

const error: any = catchError(() => middleware.use(request as any));

expect(error.message).to.eq("You must accept content-type application/json");
Expand Down
16 changes: 14 additions & 2 deletions packages/integration/test/bodyparams.spec.ts
@@ -1,5 +1,5 @@
import {expect} from "chai";
import {Controller, Post, Required, PlatformTest, BodyParams} from "@tsed/common";
import {BodyParams, Controller, PlatformTest, Post, Required} from "@tsed/common";
import * as SuperTest from "supertest";
import {TestServer} from "./helpers/TestServer";

Expand Down Expand Up @@ -78,7 +78,19 @@ describe("Body spec", () => {
it("should return an empty array (1)", async () => {
const response = await request.post("/rest/test-scenario-3").send().expect(400);

expect(response.text).to.deep.equal("Bad request on parameter \"request.body.test\".<br />It should have required parameter 'test'");
expect(response.body).to.deep.equal({
"name": "REQUIRED_VALIDATION_ERROR",
"message": "Bad request on parameter \"request.body.test\".\nIt should have required parameter 'test'",
"status": 400,
"errors": [{
"dataPath": "",
"keyword": "required",
"message": "It should have required parameter 'test'",
"modelName": "body",
"params": {"missingProperty": "test"},
"schemaPath": "#/required"
}]
});
});
});
});
15 changes: 0 additions & 15 deletions packages/integration/test/data/swagger.spec.json
Expand Up @@ -70,9 +70,6 @@
"responses": {
"200": {
"description": "Success"
},
"401": {
"description": "Unauthorized"
}
}
}
Expand Down Expand Up @@ -260,9 +257,6 @@
},
"400": {
"description": "Missing required parameter"
},
"401": {
"description": "Unauthorized"
}
}
},
Expand Down Expand Up @@ -567,9 +561,6 @@
},
"400": {
"description": "Missing required parameter"
},
"401": {
"description": "Unauthorized"
}
},
"security": [
Expand Down Expand Up @@ -617,9 +608,6 @@
"responses": {
"200": {
"description": "Success"
},
"401": {
"description": "Unauthorized"
}
}
}
Expand All @@ -633,9 +621,6 @@
"responses": {
"200": {
"description": "Success"
},
"401": {
"description": "Unauthorized"
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/integration/test/query.spec.ts
@@ -1,5 +1,5 @@
import {expect} from "chai";
import {Controller, PlatformTest, Get, QueryParams} from "@tsed/common";
import {Controller, Get, PlatformTest, QueryParams} from "@tsed/common";
import * as SuperTest from "supertest";
import {TestServer} from "./helpers/TestServer";

Expand Down Expand Up @@ -134,7 +134,12 @@ describe("Query spec", () => {
it("should throw bad request", async () => {
const response = await request.get(`${endpoint}?test=error`).expect(400);
// FIXME REMOVE THIS when @tsed/schema is out
expect(response.text).to.deep.equal("Bad request on parameter \"request.query.test\".<br />Cast error. Expression value is not a number.");
expect(response.body).to.deep.equal({
"name": "VALIDATION_ERROR",
"message": "Bad request on parameter \"request.query.test\".\nCast error. Expression value is not a number.",
"status": 400,
"errors": []
});
});
it("should return undefined when query is empty", async () => {
const response = await request.get(`${endpoint}?test=null`).expect(200);
Expand Down
7 changes: 6 additions & 1 deletion packages/integration/test/response.spec.ts
Expand Up @@ -261,7 +261,12 @@ describe("Response", () => {
it("should throw a badRequest when path params isn't set as number", async () => {
const response = await request.get("/rest/response/scenario9/kkk").expect(400);

expect(response.text).to.equal("Bad request on parameter \"request.path.id\".<br />Cast error. Expression value is not a number.");
expect(response.body).to.deep.equal({
"message": "Bad request on parameter \"request.path.id\".\nCast error. Expression value is not a number.",
"status": 400,
"name": "VALIDATION_ERROR",
"errors": []
});
});
});
});
Expand Down

0 comments on commit f0d9d46

Please sign in to comment.