Skip to content

Commit

Permalink
Support custom error handling and support stack trace disabling from …
Browse files Browse the repository at this point in the history
…environment variable #18
  • Loading branch information
lazarv committed Mar 5, 2018
1 parent 8fa14b1 commit 2be05c0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "odata-v4-server",
"version": "0.2.10",
"version": "0.2.11",
"description": "OData V4 Server",
"main": "build/lib/index.js",
"typings": "build/lib/index",
Expand Down
7 changes: 6 additions & 1 deletion src/example/advanced.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MongoClient, ObjectID, Db } from "mongodb";
import { createQuery } from "odata-v4-mongodb";
import { Edm, odata, ODataController, ODataServer, ODataQuery, createODataServer } from "../lib/index";
import { Edm, odata, ODataController, ODataServer, ODataQuery, createODataServer, ODataErrorHandler } from "../lib/index";
import { Category, Product, NorthwindTypes } from "./model";
import { Writable } from "stream";
let categories = require("./categories");
Expand Down Expand Up @@ -80,6 +80,11 @@ export class NorthwindODataServer extends ODataServer{
await categoryCollection.insertMany(categories);
await productsCollection.insertMany(products);
}

static errorHandler(err, req, res, next){
delete err.stack;
ODataErrorHandler(err, req, res, next);
}
}

createODataServer(NorthwindODataServer, "/odata", 3000);
Expand Down
10 changes: 10 additions & 0 deletions src/lib/odata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "reflect-metadata";
import { Token, TokenType } from "odata-v4-parser/lib/lexer";
import { ErrorRequestHandler } from "express";
import { ODataServer } from "./server";
import { ODataController } from "./controller";
import { EntityType } from "./edm";
Expand Down Expand Up @@ -140,6 +141,15 @@ export function validation(validator: IODataValidator, options: IODataValidatorO
};
}

/** Set error handler
* @param errorHandler Error request handler to use
*/
export function error(errorHandler: ErrorRequestHandler) {
return function (target: typeof ODataServer) {
target.errorHandler = errorHandler;
};
}

/** Class decorator for server that binds the given controller to the server.
* @param controller Controller to be bind to the server.
* @param isPublic Is the binding public or not.
Expand Down
5 changes: 3 additions & 2 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class ODataServerBase extends Transform{
static parser = ODataParser;
static connector:IODataConnector
static validator:(odataQuery:string | Token) => null;
static errorHandler:express.ErrorRequestHandler = ODataErrorHandler;
private serverType:typeof ODataServer

static requestHandler(){
Expand Down Expand Up @@ -285,7 +286,7 @@ export class ODataServerBase extends Transform{
}, server.document().requestHandler());
router.get("/\\$metadata", server.$metadata().requestHandler());
router.use(server.requestHandler());
router.use(ODataErrorHandler);
router.use(server.errorHandler);

if (typeof path == "number"){
if (typeof port == "string"){
Expand Down Expand Up @@ -317,7 +318,7 @@ export function ODataErrorHandler(err, _, res, next){
error: {
code: statusCode,
message: err.message,
stack: err.stack
stack: process.env.ODATA_V4_DISABLE_STACKTRACE ? undefined : err.stack
}
});
}else next();
Expand Down

0 comments on commit 2be05c0

Please sign in to comment.