[Question] UncaughtExceptionHandler

Discussion in 'Spigot Plugin Development' started by xxxCheeseproxxX, May 25, 2015.

  1. Hello again, I am trying to create an error uploader plugin that uploads whatever the occurred error was of whatever plugin to hastebin.com by its API.

    And I think the UncaughtExceptionHandler thing that was in the Java JDK could "listen to" and "report" any errors on any thread.

    This is what I have right now, but I wonder if Spigot has a specific thread I need to put in.


    Code (Text):
    Thread myThread = new Thread(//What should I put here?);
    myThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
         public void uncaughtException(Thread myThread, Throwable e) {
         System.out.println(myThread.getName() + " throws exception: " + e);
         }
    });


    Any question related replies are highly appreciated :). Thank you
     
  2. You could use
    Code (Text):
    System.out.println(Thread.currentThread().getName());
    System.out.println(Thread.currentThread().getId());
    To try and figure out what the main thread's called.

    EDIT:
    I did some research and it looks like you should use:
    Code (Text):
    Thread.setDefaultUncaughtExceptionHandler(handler);
     
    • Agree Agree x 1
  3. Half the time, if not all the time, these exceptions won't reach your UncaughtExceptionHandler. They are handled in their specific try-catch blocks, and passed to the logger.

    However, you should be able to attach a filter to that Logger which checks if it's reporting an exception, and cancel the logging there (or replace the message with a String) and handle the exception yourself.
     
  4. Sorry but I couldn't understand what do you mean by attach a filter to that Logger.

    Do you mean this filter? (https://docs.oracle.com/javase/7/docs/api/java/util/logging/Filter.html)

    And this Logger? (http://docs.oracle.com/javase/7/docs/api/java/util/logging/Logger.html?is-external=true)


    If so, how would I use them together? Sorry for asking too much, I am kinda a noob at this xP
     
  5. Thank you for researching for me :), but what should the "handler" in Thread.setDefaultUncaughtExceptionHandler(handler) be?
     
  6. An uncaught exception handler like the one you made in your first post, however you should ignore what I said and try to do what DarkSeraphim said. I don't use the Logger a whole lot so I can't help you there.
     
  7. 1. Create a new LogHandler
    2. Call the attach method
    3. Handle the console output & filter the input for keywords like "exception" etc. (see example below)
    4. Upload your stuff to hastebin

    Code (Text):
    import java.util.logging.Handler;
    import java.util.logging.LogRecord;

    import org.bukkit.Bukkit;


    public class LogHandler extends Handler{

        public void attachToBukkit(){
            Bukkit.getLogger().addHandler(this);
        }
       
       
        @Override
        public void close() throws SecurityException {
           
        }

        @Override
        public void flush() {
           
        }

        @Override
        public void publish(LogRecord arg0) {
            if(arg0.getMessage().contains("Exception")){
                //upload dis
            }
        }
       
    }
     
    • Like Like x 1
    • Useful Useful x 1
  8. Thank you, but what should I put in publish(//here?)

    The problem is that I don't know how to handle the console output... is it like getLogger().getSomething() but I can't find the relevant method.
     
    #9 xxxCheeseproxxX, May 27, 2015
    Last edited: May 27, 2015
  9. This LogHandler gets the output of every single line. So when you got a random exception...:

    net.java.RandomCrappyException: You did something wrong
    at bla1
    at bla2
    at bla3

    ...Every line gets passed to you as a LogRecord in the publish function. Then you simply got to put all of them together (Just add them all in a String) and then upload this String to pastebin using the jPastebin library:

    https://github.com/BrianBB/jPastebin
     
  10. md_5

    Administrator Developer

    Please use the scheduler not threads.
     
    • Agree Agree x 2
    • Funny Funny x 1
    • Optimistic Optimistic x 1