Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch exception from Classes doesn't work #68

Open
nicutor opened this issue Oct 2, 2018 · 7 comments
Open

Catch exception from Classes doesn't work #68

nicutor opened this issue Oct 2, 2018 · 7 comments

Comments

@nicutor
Copy link

nicutor commented Oct 2, 2018

Hi,

I have this test script with 4 ways to execute mkdir command (for example):

#!/usr/bin/env bash
source "$( cd "${BASH_SOURCE[0]%/*}" && pwd )/lib/oo-bootstrap.sh"
import util/log util/exception util/tryCatch util/namedParameters util/class

# Creating a Test class
class:Test() {
  Test.Mkdir() {
    [string] path
    if [ ! -d "${path}" ]; then
      mkdir "${path}"
    fi
  }

  Test.MkdirTryCatch() {
    [string] path
    if [ ! -d "${path}" ]; then
      try {
        mkdir "${path}"
      } catch {
        echo "There was an error on folder creation!"
        echo "Caught Exception:$(UI.Color.Red) $__BACKTRACE_COMMAND__ $(UI.Color.Default)"
        echo "File: $__BACKTRACE_SOURCE__, Line: $__BACKTRACE_LINE__"
        Exception::PrintException "${__EXCEPTION__[@]}"
      }
    fi
  }
}

Type::Initialize Test
Test Command

# Run mkdir command without class
#mkdir /tmp/a/b

# Run mkdir command without class with try and catch
#try {
#  mkdir "${path}"
#} catch {
#  echo "There was an error on folder creation!"
#  echo "Caught Exception:$(UI.Color.Red) $__BACKTRACE_COMMAND__ $(UI.Color.Default)"
#  echo "File: $__BACKTRACE_SOURCE__, Line: $__BACKTRACE_LINE__"
#  Exception::PrintException "${__EXCEPTION__[@]}"
#}

# Run mkdir command using the Test class
#$var:Command Mkdir "/tmp/a/b"

# Run mkdir command using the Test class with try and catch
#$var:Command MkdirTryCatch "/tmp/a/b"

1. Run mkdir command without class

mkdir /tmp/a/b

Output:

# ./test.sh
mkdir: cannot create directory â/tmp/a/bâ: No such file or directory

 â UNCAUGHT EXCEPTION: __typeCreate_paramNo (1)
   î  â¦ mkdir /tmp/a/b [test.sh:33]

 â¡ Press [CTRL+C] to exit or [Return] to continue execution.

2. Run mkdir command without class with try and catch

try {
  mkdir "${path}"
} catch {
  echo "There was an error on folder creation!"
  echo "Caught Exception:$(UI.Color.Red) $__BACKTRACE_COMMAND__ $(UI.Color.Default)"
  echo "File: $__BACKTRACE_SOURCE__, Line: $__BACKTRACE_LINE__"
  Exception::PrintException "${__EXCEPTION__[@]}"
}

Output:

# ./test.sh
mkdir: cannot create directory ââ: No such file or directory
There was an error on folder creation!
Caught Exception: mkdir "${path}"
File: test.sh, Line: 37
   î  â¦ mkdir "${path}" [test.sh:37]

3. Run mkdir command using the Test class

$var:Command Mkdir "/tmp/a/b"

Output:

# ./test.sh
mkdir: cannot create directory â/tmp/a/bâ: No such file or directory

4. Run mkdir command using the Test class with try and catch

$var:Command MkdirTryCatch "/tmp/a/b"

Output:

# ./test.sh
mkdir: cannot create directory â/tmp/a/bâ: No such file or directory
There was an error on folder creation!
Caught Exception:
File: /usr/local/share/bash-oo-framework/lib/util/command.sh, Line: 58

The problem is that, the exception/error is not cought when I am using the Test Class.
Is there something I need to set/import/etc. ? I am missing something?

Please guide me in the right direction.

Thank you!

@niieani
Copy link
Owner

niieani commented Oct 5, 2018

Hi @nicutor. This looks like a bug. I'm guessing the return code of the method is lost and not being passed correctly.

After a quick look I think the issue is here:
https://github.com/niieani/bash-oo-framework/blob/master/lib/util/type.sh#L435

It seems to be falling back to a boolean too eagerly after a > 0 status code.

But I haven't touched these parts in a while so I might be wrong.

Sadly, I don't have the time to debug this further at this point.

@nicutor
Copy link
Author

nicutor commented Oct 5, 2018

Hi @niieani . Thank you for your reply.

I am so sorry that there can't be a quick fix for that, as I was so decided to use this framework for my things. But now.. if the main thing (exception) is not working, I am not so sure that I will start to write all my new scripts using it. And for sure, after I will write them, I will not have time to rewrite them again when the framework will be fixed.. All my plans was ruined :(

Its really a good work, congrats for it.

@niieani
Copy link
Owner

niieani commented Oct 5, 2018

Sorry to hear that, @nicutor. :(
I wish the reality was different, but this is a free time project for me.

I will add a mark in the README to hold off major work based on the framework before we do a proper rewrite with 3.0 (#45).

@nicutor
Copy link
Author

nicutor commented Oct 5, 2018

There is no problem, I can totally understand you and I really appreciate your work! I am just annoyed that I found such a good library and I am not able to use it because of some small bugs.. :( But this is it :)

@niieani
Copy link
Owner

niieani commented Oct 5, 2018

Sure thing! I wrote this more as an experiment a few years ago and the framework exploded just recently after somebody posted it this year to HackerNews (seriously, 3400 stars in a few days...!).

I don't think it's ready for serious projects (mainly due to missing test coverage, which could have find out about errors such as this one), so I've now added a disclaimer saying so.

@Cj-bc
Copy link
Contributor

Cj-bc commented Nov 20, 2018

I searched this bug by using git bisect.
But... I don't think I got a valid result, because the left commit was def7373 , which just adds docs and renames some variables(etc).
So I guess that this bug was there from really early stage of this framework.
It means, I think @niieani 's this comment could be where bug is living:

After a quick look I think the issue is here:
https://github.com/niieani/bash-oo-framework/blob/master/lib/util/type.sh#L435

It seems to be falling back to a boolean too eagerly after a > 0 status code.

It seems to be difficult to solve this :(
Fortunately, I'm a student and have some free time. So I can work for this problem!!!
I'll try to figure out/fix this bug ( after more code-reading)

@niieani
Copy link
Owner

niieani commented Nov 25, 2018

Amazing, thanks @Cj-bc, I really appreciate this. If you need any insights on the code, feel free to ask questions here, or we can chat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants