Skip to content

Catch exception from Classes doesn't work #68

@nicutor

Description

@nicutor

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions