Coder Social home page Coder Social logo

think-trace's Introduction

think-trace

npm Travis Coveralls David

think-trace is an error handler for ThinkJS 3.x and koa2. It provides a pretty error web interface that helps you debug your web project.

Installation

npm install think-trace

How To Use

Koa2

const traceMiddleware = require('think-trace');
app.use(traceMiddleware({
  sourceMap: false,
  error: err => console.error(err)
}));

ThinkJS3.x

Modify src/config/middleware.js:

const trace = require('think-trace');

module.exports = [
  {
    handle: trace, 
    options: {
      sourceMap: false,
      error(err, ctx) {
        return console.error(err);
      }
    }
  }
];

Options

  • sourceMap: Whether your project has source map support, default is true.
  • debug: Whether show error detail in web, default is true.
  • ctxLineNumbers: How long you want show error line context, default is 10.
  • contentType: Due to think-trace can't get content-type while application throw error, you should set content type by yourself by this parameter. Default value is ctx => 'html';. You can set json content type like this:
    {
      contentType(ctx) {
        // All request url starts of /api or request header contains `X-Requested-With: XMLHttpRequest` will output json error
        const APIRequest = /^\/api/.test(ctx.request.path);
        const AJAXRequest = ctx.is('X-Requested-With', 'XMLHttpRequest');
        
        return APIRequest || AJAXRequest ? 'json' : 'html';
      }
    }
  • error: callback function when catch error, it receives Error object and ctx as parameter.
  • templates: error status template path, if you want to specific. You can set templates as a path string, then module will read all status file named like 404.html, 502.html as your customed status page. Or you can set templates as an object, for example:
    {
      options: {
        //basic set as string, then put 404.html, 500.html into error folder
        templates: path.join(__dirname, 'error'),
    
        //customed set as object
        templates: {
          404: path.join(__dirname, 'error/404.html'),
          500: path.join(__dirname, 'error/500.html'),
          502: path.join(__dirname, 'error/502.html')
        }
      }
    }
    Also you can set templates as function, function should return error template file path or error template file path object.

Contributing

Contributions welcome!

License

MIT

think-trace's People

Contributors

dsonet avatar lizheming avatar welefen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.