It’s annoying that the HttpService JSONDecode method is not fail-safe, meaning that if it is given any invalid JSON then it asserts an nondescript error and stops everything. It should do something more sensible like return nil instead if invalid JSON is given.
My suggestion:
Either tell what is wrong with the given JSON, output the given JSON, or don’t have it break everything. It’s such a nondescript error and it does not help debug nor make things easier to program.
There are two completely separate issues - 1. the error message for incorrect JSON data does not say where exactly the data is wrong, 2. instead of returning nil, failed JSON data generates an error.
Note that 2. does not mean that Roblox crashes - it just terminates the calling Lua thread unless you use pcall.
We will probably fix issue 1.
Why do you need nil if you can always use pcall/ypcall?
There are two completely separate issues - 1. the error message for incorrect JSON data does not say where exactly the data is wrong, 2. instead of returning nil, failed JSON data generates an error.
Note that 2. does not mean that Roblox crashes - it just terminates the calling Lua thread unless you use pcall.
We will probably fix issue 1.
Why do you need nil if you can always use pcall/ypcall? [/quote]
pcall/ypcall is a bad practice. Any method which can error should always return nil with an error message on error.
Unfortunately, methods follow the convention of only being able to return one value. Yes, they can return a Tuple, which interfaces with Lua’s multiple return values. But that hides the actual underlying value, which is not so neat when the method is generally not throwing errors.
The only other good way to propagate an error is to just throw an error, which can be picked up by pcall. This appears to be how ROBLOX does error handling, even if they don’t mention it anywhere. Whatever hit in performance that is taken by using pcall is probably fine for most methods. Methods that require speed (i.e. Terrain.SetCell) are designed to never throw errors. Well, at least I hope they are.
That said, what should be made clear is whether a method can potentially throw an error. A “canError” tag in the API dump would be nice.
Unfortunately, methods follow the convention of only being able to return one value. Yes, they can return a Tuple, which interfaces with Lua’s multiple return values. But that hides the actual underlying value, which is not so neat when the method is generally not throwing errors.
The only other good way to propagate an error is to just throw an error, which can be picked up by pcall. This appears to be how ROBLOX does error handling, even if they don’t mention it anywhere. Whatever hit in performance that is taken by using pcall is probably fine for most methods. Methods that require speed (i.e. Terrain.SetCell) are designed to never throw errors. Well, at least I hope they are.
That said, what should be made clear is whether a method can potentially throw an error. A “canError” tag in the API dump would be nice.[/quote]
Consider it a form of documentation. I guess it doesn’t have to be done through the API dump, but it would still be nice to have.
I remember this. Back in the day, people decided using pcall was bad practice because they thought that it would let people get away with writing sloppy code, which should be written to not throw errors in the first place. I don’t think fact that ROBLOX methods can throw errors was ever involved.