GithubHelp home page GithubHelp logo

error-tracer's Introduction

Error-Tracer

DeepScan grade

Error-Tracer will help to trace client errors.

ErrorTracer

  1. Collect client error.
    Users don't report an error. They just go away and forget the error.

  2. Capture the source code when error occurred.
    Sometimes source codes are overwritten. So it makes difficult to recognize what has been the problem.

  3. Snapshot of user's environment when error occurred.
    What was the user browser? What was the data in local/sessionStorage or cookie? What time was error occurred?

Install

npm install --save error-tracer
yarn add -D error-tracer

or browser

<script src="https://unpkg.com/error-tracer@latest/dist/errortracer.bundle.js"></script>

Import

import ErrorTracer from 'error-tracer';
const ErrorTracer = require('error-tracer');

or browser

<script src="https://unpkg.com/error-tracer@latest/dist/errortracer.bundle.js"></script>

Usage

with Constructor

Note that default trigger error events are 'error', 'unhandledrejection', 'rejectionhandled'.

  1. object (you can set triggers(error), callback, apiURL, sourceRange and ignores)
new ErrorTracer({
  triggers: ['my_custome_error1', 'other_error'], // error events which observed
  callback: function(e) { console.log(1, e) },
  apiURL: "http://aaa.com",
  sourceRange: 30, // line range will be captured (default: 10)
  ignores: "error_message"
  // ignores: ["Custom_Error1", Customer_Error2"]
  // detail: userId, // # you can set extra detail information
});
  1. function (you can set just only callback function)
new ErrorTracer(function (errorItem) {
  console.error("this is handled by error-tracer", errorItem);
});
  1. string (you can set just apiURL which get report)
new ErrorTracer("http://xxx.com..."); // ErrorTracer Item will be posted

with init method

same usage with constructor

const errorTrace1 = new ErrorTracer();

errorTrace1.init(function (errItem) {
  console.log("errItem: ", errItem);
});

API

Methods

Name Type parameter description
init(parameter) Function object, function, string Initialize ErrorTracer
active() Function none Activate ErrorTracer (default: true)
deactive() Function none Deactivate ErrorTracer
push(error) Function error Manually input error
history Array[Object] none ErrorTracer History

Parameters for init

Name Type description Example
triggers Array[String] Error events will be observed. default ['error', 'unhandledrejection', 'rejectionhandled'] ['my_custome_error1', 'other_error']
callback Function callback function for errorItem function(e) { console.log("ErrorTracer Catch:" ,e); }
apiURL String if assigned, errorItem will be passed "https://zapier..."
sourceRange Integer The range of source code will be captured at around error 30 (Above 15 lines and Below 15 lines)
ignores Array[String] Error message will be ignored in ErrorTracer ["Custom_Error1", Customer_Error2"]
detail Any Information what you want to add userId, sessionId, remote_ip,... whatever

ErrorTracer will return below information

Below errorItem will be passed to callback/apiURL.

ErrorTracer will return 'source' info only when error has the filename and lineNo.

Name Type Description
errorId String Unique Error Id
clientId String Unique Client Id
error Object Original Error Event Object
location String Location which error occurred
*source Array[Object] Source code around of error. Object contains 'lineNo' and 'content'
errorLineNo Integer Line number of source code
environment Object navigator, localStorage, sessionStorage, cookie
detail Any detail info which you set
timeStamp Time Date.now()

ErrorTracer History

const errorTracer = new ErrorTracer(function(errorItem) {
  console.log("error occured: ", errorItem);
});

// history, result is array
console.log(errorTracer.history);

Examples

#1 Send ZapierWebHook

You can easily collect error with Zapier. Trigger with "Catch Hook" and make action like "Send Gmail".

<script>
  document.querySelector("#triggerError")
    .addEventListener("click", function () {
      notExist(); // make Error
    });

  const url = "http://xxxx..."; // Your Zapier Webhook URL

  new ErrorTracer({
    apiURL: url,
  });

  // or simpley
  new ErrorTracer(url);
</script>

zapier

Now when Error occured, you can get report via Gmail about the error.

#2 Use with VueJS

Create an instance in 'main.js' (or any file which vuejs is imported) pass the 'vuejs error' to ErrorTracer.

ErrorTracer will return 'source' info only when error has the filename and lineNo.
Unfortunately ErrorTracer can't return 'source' info with Vue (Vue can't give those info).

// import ErrorTracer
import ErrorTracer from 'error-tracer'

// create errorTracer Instance
const myTracer = new ErrorTracer(function(err) {
  console.log("ErrorTracerCathch!: ", err);
})

// Pass the 'Vue Error' to ErrorTracer
Vue.config.errorHandler = function(err, vm, info) {
  myTracer.push(err); // <-- Yes Here! :)
}

Author

[email protected]

License

MIT

error-tracer's People

Contributors

devjiro76 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

amit08255

error-tracer's Issues

TO DO

  1. availablity to specify "error event" which error tracer will catch.

  2. let get source and send API work at worker

Check strict mode of sendApi()

Dear,
I just checked errortracer.js code in my static analyzer. (https://deepscan.io/demo/)
It tells sendApi() can have a problem that this is undefined in strict mode.
image

ES6 module is implicitly in strict mode and the bundled file has 'use strict' explicitly.

So I ran a basic code in the browser and this.apiURL can't be accessed because this is undefined.
I think sendApi() should have apiURL as its parameter instead of accessing this.
Please check :)

<button id="triggerError">triggerError</button>

<script src="errortracer.bundle.js"></script>

<script>
  document.querySelector("#triggerError")
    .addEventListener("click", function () {
      notExist(); // make Error
    });

  const url = "http://xxxx..."; // Your Zapier Webhook URL

  new ErrorTracer({
    apiURL: url,
  });

  // or simpley
  new ErrorTracer(url);
</script>

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.