Skip to content

Pin down return value on failure #821

Closed

Description

@qaisjp

Recently there seems to be some confusion about what values functions should return when they encounter an error.

Historically, the following has been the general policy; (1):

  • On failure, all functions should return false.

Somewhere along the way we realised functions that ordinarily return booleans (isPedDead, hasObjectPermissionTo, etc) should not return meaningful values (false) when errors are encountered, so we started doing something else for new functions; (2):

  • If a successful function call would return a boolean value, a failed call should return nil.
  • Otherwise, all other failed function calls return false.

Some new functions / pull requests have started to do something else; (3):

  • Always return nil on failure, no matter the return type.

Things to pin down:

  • Pick a system
    1. Stick to the current system (2)
    2. Begin adopting (3)?
    3. Something else?
  • Do we want to modify old functions or should this only apply to new functions?

Activity

  1. added this to the 1.5.7 milestone on Feb 17, 2019
  2. qaisjp commented on Feb 17, 2019

    @qaisjp
    MemberAuthor

    Okay, so my current POV is that (3) is a good way to go. I think it's nice because it's consistent for every function.

    Nevertheless, I have been asking for PRs to stick to (2) since that's what is currently done.

    I am unsure as to whether existing functions should change. I imagine a lot of existing code does if result == false instead of if not result.

  3. botder commented on Feb 17, 2019

    @botder
    Member

    What do you think about this one?

    int CLuaACLDefs::hasObjectPermissionTo(lua_State* luaVM)
    {
    ...
    // Failed
    lua_pushnil(luaVM);
    return 1;
    }

    I prefer to return nil on failure, no matter the return type (3).
    I am not sure if changing existing functions, to return nil, would break scripts.

  4. qaisjp commented on Feb 17, 2019

    @qaisjp
    MemberAuthor

    hasObjectPermissionTo is a getter that returns boolean values, so fits into (2).

  5. botder commented on Feb 17, 2019

    @botder
    Member

    It's not actually a getter, because by definition, it doesn't simply return a private member variable of the player/resource/..., which gets passed to the function. It has to lookup the permission in the ACL for the object and then returns a boolean.

  6. qaisjp commented on Feb 17, 2019

    @qaisjp
    MemberAuthor

    (2) isn't restricted to getters in that traditional sense, it is more about false having a special meaning.

    So because false has a special meaning in hasObjectPermissionTo, (i.e. "no, this object doesn't have permission"), it fits into (2).

    I've edited this issue to reflect this.

    In fact, hasObjectPermissionTo was ahead of its time, doing (2) before we noticed we needed to do (2).

  7. sbx320 commented on Feb 17, 2019

    @sbx320
    Member

    I'd prefer another option: Hard error on usage mistakes. hasObjectPermissionTo(false) is obviously wrong and needs to be fixed at the call site. Relying on results from that call is also fairly pointless and regularly causes trouble in other parts of a script, for example by adding results to a table, which then incorrectly contains false instead of strings. This would also be more consistent with built-in Lua functions.

    For new functions this could be applied easily. Applying this to old functions is a bit tricky since it could break badly written scripts and we'd be willing to break backwards compat (yet another reason for 1.6 ;)) with (wrong) code like setElementPosition(vehicle, "potato") setElementHealth(vehicle, 1000), which would currently throw a warning, but still set the health to 1000. With a hard error, setElementHealth would not be executed.

  8. qaisjp commented on Feb 18, 2019

    @qaisjp
    MemberAuthor

    I would be in support of just erroring. Are there any other situations where we might want to use warnings, or would this eradicate the use of warnings completely? (Ignoring existing functions.)

  9. botder commented on Feb 18, 2019

    @botder
    Member

    We could also add a "strict" mode, which could, if enabled, throw an error for already existing functions.

  10. qaisjp commented on Feb 25, 2019

    @qaisjp
    MemberAuthor

    Okay, so let's hard error on usage mistakes, as per #821 (comment).

    Let's also do something like a strict mode (#821 (comment))... how do we want to implement this?

    Potential ways would be:

    • per-resource meta <strict>true</strict>
    • server-wide
    • some others
  11. botder commented on Feb 25, 2019

    @botder
    Member

    We could add server-wide, per-resource and per-script. Everyone would be happy.

  12. qaisjp commented on Feb 25, 2019

    @qaisjp
    MemberAuthor

    Haha that's a joke right?

  13. botder commented on Feb 25, 2019

    @botder
    Member

    Yes, only server-wide and per-resource is enough.

  14. forkerer commented on May 18, 2019

    @forkerer
    Contributor

    It's been 3 months, has there been any progress with this? It's blocking a few pull request and doesn't seem like it's going to change soon

  15. qaisjp commented on May 18, 2019

    @qaisjp
    MemberAuthor

    @sbx320 has been working on this — am I fine to assign this to you?

  16. qaisjp commented on May 22, 2019

    @qaisjp
    MemberAuthor

    To clarify the functionality mentioned here is blocking pull requests:

    Okay, so let's hard error on usage mistakes, as per #821 (comment).

    "Strict mode" can be discussed and added later as a separate issue/feature.

  17. sbx320 commented on May 25, 2019

    @sbx320
    Member

    Current state: Stuff works, I've gotten stuck on some performance isuse, but that is resolved now. Only missing part now is to handle errors properly (right now the errors are handled, but the messages are empty). Plus a whole bunch of testing obviously.

  18. botder commented on Aug 17, 2019

    @botder
    Member

    Okay, so let's hard error on usage mistakes, as per #821 (comment).

    Let's also do something like a strict mode (#821 (comment))... how do we want to implement this?

    Potential ways would be:

    • per-resource meta <strict>true</strict>
    • server-wide
    • some others

    I am going to close this issue now. See quote for final decision.

    Before sbx320's pull request is done, the following code should be fine:

    if (argStream.HasErrors())
    {
        return luaL_error(luaVM, argStream.GetFullErrorMessage());
    }

    (Docs: http://pgl.yoyo.org/luai/i/luaL_error)

  19. qaisjp commented on Aug 17, 2019

    @qaisjp
    MemberAuthor

    Sounds good!

  20. ccw808 commented on Aug 17, 2019

    @ccw808
    Member

    What about gathering stats to measure the impact of causing hard errors. i.e. Servers and clients upload info about usage mistakes

  21. AlexTMjugador commented on Aug 17, 2019

    @AlexTMjugador
    Member

    I can't find any downside to gathering more anonymized information with consent of the server owners in order to make a better decision 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions