GithubHelp home page GithubHelp logo

ninjayoto / spark-debug-tools Goto Github PK

View Code? Open in Web Editor NEW

This project forked from perwendel/spark-debug-tools

0.0 1.0 0.0 103 KB

Better error pages for the Spark Java micro-framework.

License: Apache License 2.0

Java 37.24% FreeMarker 30.39% CSS 21.96% JavaScript 10.41%

spark-debug-tools's Introduction

Spark Debug Screen

Error pages for the Spark Java micro-framework.

image

Usage:

To utilize:

package spark.debug;

import static spark.Spark.get;
import static spark.Spark.port;
import static spark.debug.DebugScreen.enableDebugScreen;

public class DebugScreenExample {
    public static void main(String[] args) {

        get("*", (req, res) -> {
            throw new Exception("Exceptions everywhere!");
        });
        
        enableDebugScreen(); //just add this to your project to enable the debug screen
        
    }
}

Maven:

<dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-debug-tools</artifactId>
    <version>0.5</version>
</dependency>

Advanced Usage:

To add additional tables:

// Subclass the handler:
class MyDebugScreen extends DebugScreen {
  @Override
  protected void installTables(LinkedHashMap<String, Map<String, ? extends Object>> tables, Request request, Exception exception) {
    super.installTables(tables, request, exception);
    Map<String, Object> myTable = new LinkedHashMap<>();
    tables.put("My Table", myTable);
    myTable.put("Key", "Value");
  }
}

// When installing the exception handler, install yours instead:
Spark.exception(Exception.class, new MyDebugScreen());

To change the search path for locating Java source files:

By default DebugScreen looks within the folders src/main/java and src/test/java in the current working directory (if they exist). If you have changed the working directory, obviously this approach will not work. You can specify different search directories:

Spark.exception(Exception.class, new DebugScreen(
    ImmutableList.of(new FileSearchSourceLocator(new File("/path/to/source/code")))
));

You can specify multiple locators in the list (later ones are used as fallbacks if earlier ones cannot find a file). If this is still not specific enough for you, you can implement your own SourceLocator to find the files and provide that to the handler.

Notes:

  • This handler reveals server internals and possibly code. Only install it when you are developing and make sure to disable it before pushing to production.
  • Finding code snippets is an imperfect art since the original file locations are not preserved in the compiled bytecode. Thus, by default code snippets will only be displayed if the Java source file for the corresponding exception stack frame is available within the working directory under src/main/java or src/main/test and you are properly following the Java naming and directory structure conventions. See advanced usage to change this behavior.
  • Registering the exception handler for Exception.class serves as a catch-all. You can still install your own, more specific exception handlers. Spark prioritizes exception handlers by crawling up the class hierarchy from the exception thrown to Exception looking for a handler. Thus, handlers for subclasses of Exception will take precendence.

Credits:

By mschurr. Based on filp/whoops.

spark-debug-tools's People

Contributors

tipsy avatar mschurr avatar pelenthium avatar

Watchers

James Cloos 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.