This commit is contained in:
emmanuel
2015-11-19 11:51:30 +01:00
6 changed files with 97 additions and 6 deletions
+10
View File
@@ -5,3 +5,13 @@ import errorhandler = require('errorhandler');
var app = express();
app.use(errorhandler());
app.use(errorhandler({ log: true }));
app.use(errorhandler({ log: (err, str, req, res) => {
const { message, name, stack } = err;
const messageIsStr = message === str;
const requestWasFresh = req && req.fresh;
const responseContentType = res && res.contentType
}}))
+22 -2
View File
@@ -7,6 +7,26 @@
declare module "errorhandler" {
import express = require('express');
function e(options?: {log?: any}): express.ErrorRequestHandler;
export = e;
function errorHandler(options?: errorHandler.Options): express.ErrorRequestHandler;
namespace errorHandler {
interface LoggingCallback {
(err: Error, str: string, req: express.Request, res: express.Response): void;
}
interface Options {
/**
* Defaults to true.
*
* Possible values:
* true : Log errors using console.error(str).
* false : Only send the error back in the response.
* A function : pass the error to a function for handling.
*/
log: boolean | LoggingCallback;
}
}
export = errorHandler;
}
@@ -0,0 +1,26 @@
/// <reference path="flux-standard-action.d.ts" />
//import action = require('flux-standard-action');
import { isError, isFSA, Action, ErrorAction } from 'flux-standard-action';
interface TextPayload {
text: string;
}
var sample1: Action<TextPayload> = {
type: 'ADD_TODO',
payload: {
text: 'Do something.'
}
};
var sample2: ErrorAction = {
type: 'ADD_TODO',
payload: new Error(),
error: true
};
var result1: boolean = isError(sample1);
var result2: boolean = isFSA(sample1);
var result3: boolean = isError(sample2);
var result4: boolean = isFSA(sample2);
+31
View File
@@ -0,0 +1,31 @@
// Type definitions for flux-standard-action 0.5.0
// Project: https://github.com/acdlite/flux-standard-action
// Definitions by: Qubo <https://github.com/tkqubo>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module "flux-standard-action" {
export interface ErrorAction extends Action<Error> {
error: boolean;
}
export interface Action<T> {
type: string;
payload?: T;
error?: boolean;
}
// Usage: var action: Action<sring> & AnyMeta;
export interface AnyMeta {
meta: any
}
// Usage: var action: Action<sring> & TypedMeta<string>;
export interface TypedMeta<T> {
meta: T
}
export function isFSA(action: any): boolean;
export function isError(action: any): boolean;
}
+1
View File
@@ -5,6 +5,7 @@ import methodOverride = require('method-override');
var app = express();
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(methodOverride('X-HTTP-Method-Override', { methods: ["GET", 'POST'] }));
app.use(methodOverride((req: express.Request, res: express.Response) => {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
+7 -4
View File
@@ -13,12 +13,15 @@ declare module Express {
declare module "method-override" {
import express = require('express');
module e {
interface MethodOverrideOptions {
namespace e {
export interface MethodOverrideOptions {
methods: string[];
}
}
function e(getter: string, options?: any): express.RequestHandler;
function e(getter: (req: express.Request, res: express.Response) => string, options?: any): express.RequestHandler;
function e(getter?: string, options?: e.MethodOverrideOptions): express.RequestHandler;
function e(getter?: (req: express.Request, res: express.Response) => string, options?: e.MethodOverrideOptions): express.RequestHandler;
export = e;
}