Coder Social home page Coder Social logo

Comments (4)

mertushka avatar mertushka commented on May 30, 2024 1

The issue you're facing is related to exception handling in different event handlers of the @basro's code. In some event handlers, exceptions are not being logged as expected, while in others, they are. Let me explain what's happening and how we can address it.

In the first example, we have an event handler called room.onRoomLink, which is a traditional function declaration. Inside this handler, we explicitly throw an exception using the throw statement. Since it's a synchronous operation, the exception is caught and logged by the event loop's error handling mechanism, and we see the output in the console as expected.

However, in the second example, we have an event handler called room.onPlayerChat, which is an arrow function. Arrow functions have an implicit return and behave differently when it comes to exception handling. When we throw an exception inside an arrow function, it doesn't get caught within the function itself. Instead, it propagates up the call stack and may not be caught by the event loop's error handling mechanism. This is why we don't see the exception logged in the console.

To address this issue and ensure consistent exception handling, we need to modify the code. The solution is to explicitly catch the exceptions within each event handler and log them to the console. We can do this by wrapping the code within a try block and catching any exceptions using a catch block.

For example, in the room.onPlayerChat event handler, we can modify the code as follows:

room.onPlayerChat = (player, msg) => {
  try {
    throw new Error('test');
  } catch (error) {
    console.error(error);
  }
};

By doing this, any exceptions thrown within the event handler will be caught and logged to the console directly within the handler itself.

You should apply this modification to other event handlers, such as room.onPlayerJoin, to ensure consistent exception handling and logging throughout your code.

Otherwise, if you just want to throw an error without error handling then you must use regular functions instead of arrow functions.

from haxball.js.

basro avatar basro commented on May 30, 2024 1

A small nitpick:

Arrow functions have an implicit return and behave differently when it comes to exception handling.

Otherwise, if you just want to throw an error without error handling then you must use regular functions instead of arrow functions.

This is not correct, arrow functions and normal functions have the same behavior regarding exceptions. If you changed the onRoomLink function to an arrow function it would behave the same here.

The real cause is that haxball is catching exceptions that escape from onPlayerChat while it does not catch exceptions from onRoomLink.

The other solution you propose is correct.

from haxball.js.

mertushka avatar mertushka commented on May 30, 2024 1

Arrow functions and normal functions have the same behavior regarding exceptions. If you changed the onRoomLink function to an arrow function it would behave the same here.

Yes you are right. Actually both arrow functions and regular functions in JavaScript have the same behavior when it comes to exception handling but I wrote it based on haxball's current exception handling. Anyways, closing this as completed.

from haxball.js.

uzayyli avatar uzayyli commented on May 30, 2024

@basro Speaking of which, please add error handling to room.sendAnnouncement (and probably sendChat too?).

If a null or any non-string value is supplied, the function throws an exception which could be easily handled in the core API.

Please handle the following examples so that it doesn't fail the whole room:

room.sendAnnouncement(3)
room.sendAnnouncement(null)

Thanks

from haxball.js.

Related Issues (20)

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.