Comments (4)
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.
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.
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.
@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)
- File is not defined HOT 4
- room.setRequireCaptcha is not a function HOT 1
- setPlayerChatFilter don't work HOT 2
- Room never comes up when I add "proxy" parameter to HBInit HOT 9
- App console looks like real time console HOT 3
- DOMException [InvalidStateError]: RTCDataChannel.readyState is not 'open' HOT 8
- FetchError - ENOTFOUND HOT 17
- error when trying to up the room HOT 4
- New error HOT 1
- Can't create a room with proxy HOT 2
- Random SIGSEGV
- Token error HOT 2
- DOMException [InvalidStateError]: RTCDataChannel.readyState is not 'open'
- New Event/Function Added HOT 2
- getAddrInfo HOT 3
- Close the room HOT 1
- Close room HOT 1
- how to add variables and functions in node in the terminal HOT 2
- how to add variables and functions in node in the terminal "Part 2" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from haxball.js.