Pass through exception in PySpark - exception

I am using a python workbook in databricks, that calls another workbook from another folder.
The first workbook should throw an exception if the second workbooks fails executing, but this does not happen. It only throws an exception, if the first workbooks fails (e.g. wrong path).
How can I pass the exception of the second workbook to the first one?
First workbook:
try:
dbutils.notebook.run("../01_load/main_exec", 0)
except Exception as e:
print("01 failed")
Second workbook:
try:
dbutils.notebook.run("../02_wbs/step_exec", 0)
except Exception as e:
print("02 failed")
If the first task fails, I want my first workbook to return "01 failed". But if the second workbook fails, I want my first workbook to return "02 failed"

If you raise an exception on notebook 2, then dbutils.notebook.run("notebook_2", 0) will throw an error as well.
#notebook_2
raise Exception("Error")
#notebook_1
try:
dbutils.notebook.run("notebook_2", 0)
except Exception as e:
raise Exception("Notebook 2 failed", e)

Related

throw .net-wrappable exception from native COM-Dll

In a native-C++-Dll (MFC): how to throw an Exception (with some additional text) that can be catched and handled from a .NET-Client?
This is the native-MFC-Dll which wants to throw an Exception:
void RGaugeTVDDataReader::LoadDataFromFile_HandleException(const CString& szPath) const
{
CString sMessage;
sMessage.Format(_T("TVD-File %s"), static_cast<LPCTSTR>(szPath));
/*1*/ AfxThrowFileException(CFileException::genericException, -1, static_cast<LPCTSTR>(sMessage));
/*2*/ throw std::runtime_error(CT2A(sMessage));
}
The Caller is a .Net-Dll which uses the native dll via a .net-generated-COM-Wrapper.
Both variants (/1/ and /2/) fall into the catch-Block of the .Net-Component but what I get here is just a ´System.Runtime.InteropServices.SEHException´ and the exceptionmessage says "External component has thrown an exception.". Included ErrorCode and HResult is 0x80004005. There is no inner exception, nothing to find about my given text and the stacktrace only contains the managed part.
So the question is: how to throw an excpetion from the native-c++-dll so that a .net-client using it via COM can at least see the message-string?
Alternative question: how to catch and handle the thrown exception in .net correctly?
regards

How to mock throw new customException?

Hope all are doing good.
I am wanted to mock one of my exception which is inside one private method like below :
private void verifyScenarios(String empid, String token) {
if (Validation if true) // Line 1 :
throw new CustomException("my message"); //Line 2
else
any code.
}
Line 1: will be true.
Line 2 : This line throwing exception because of that my junit test case is failing, Is there any way to mock line 2 and make it success.
Thanks in advance.
Such things are not possible with Mockito, however I believe that all you need is to assert (pass the test) if exception is thrown, because it is a part of your business logic.
Try #Test(expected = CustomException.class) instead of #Test if you are using JUnit 4. Test will pass only if code will throw your exception.
You can use an expected exception rule. With this solution you can easily check the error message that is thrown as well.

Why not every type are throw exceptions?

"Only the types that are inherited from the Throwable class can be thrown".
Could anybody explain me. Why not every type are throwable? If in doc there no mention about function can throw exception, it's mean that it do not have exception?
For example I thought that next try-catch block would work. But it is not.
try
{
writeln("(((((((((");
latestdtonpage = dts.sort!((a,b) => a>b).front; //latest date-time. from page.
}
catch(Exception e)
{
writeln("Can't select the latest Date from parsed date");
writeln(e);
}
But output in case of exception is next (no exception text):
(((((((((
core.exception.AssertError#C:\D\dmd2\windows\bin\..\..\src\phobos\std\array.d(73
9): Attempting to fetch the front of an empty array of DateTime
----------------
0x0051C4C9 in _d_assert_msg
0x00468E78 in pure nothrow ref #property #nogc #safe std.datetime.DateTime std.r
ange.__T11SortedRangeTAS3std8datetime8DateTimeS473app19StackOverflowParser5parse
MFAyaZ9__lambda2Z.SortedRange.front() at C:\D\dmd2\windows\bin\..\..\src\phobos\
std\range.d(8418)
0x0044F908 in void app.StackOverflowParser.parse(immutable(char)[]) at D:\code\T
rendoMetr\source\app.d(173)
0x0044F700 in app.StackOverflowParser app.StackOverflowParser.__ctor(app.DBConne
ct) at D:\code\TrendoMetr\source\app.d(150)
0x0044F199 in _Dmain at D:\code\TrendoMetr\source\app.d(33)
0x0052EDCA in D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv
0x0052ED9F in void rt.dmain2._d_run_main(int, char**, extern (C) int function(ch
ar[][])*).runAll()
0x0052ECB5 in _d_run_main
0x00470198 in main
0x005667D1 in mainCRTStartup
0x76D1336A in BaseThreadInitThunk
0x772A9F72 in RtlInitializeExceptionChain
0x772A9F45 in RtlInitializeExceptionChain
Error executing command run: Program exited with code 1
How I can throw exception in this code?
Your code throws an AssertError, indicating that dts.sort!((a,b) => a>b) is empty, and you shouldn't call .front on it. Instead, query .empty first, and act accordingly when it's true.
AssertError inherits from Error which in turn inherits from Throwable but not from Exception. So catch(Exception e) doesn't catch it. And you should not catch Errors anyway, as they indicate that the program is in an unrecoverable error-state.
AssertError in particular signals a logic bug in your program. Here: calling .front on an empty range. Don't catch AssertError, but fix your code instead.

Groovy end exception different from exception thrown

I am running into an extremely strange behavior in Groovy. When I throw an exception from a closure in a Script, the end exception that was thrown was different.
Here are the code and the details:
public class TestDelegate {
def method(Closure closure) {
closure.setResolveStrategy(Closure.DELEGATE_FIRST);
closure.delegate = this;
closure.call();
}
public static void main(String[] args) {
// Make Script from File
File dslFile = new File("src/Script.dsl");
GroovyShell shell = new GroovyShell();
Script dslScript = shell.parse(dslFile);
TestDelegate myDelegate = new TestDelegate();
dslScript.metaClass.methodMissing = {
// will run method(closure)
String name, arguments ->
myDelegate.invokeMethod(name, arguments);
}
dslScript.metaClass.propertyMissing = {
String name ->
println "Will throw error now!"
throw new MyOwnException("errrrror");
}
dslScript.run();
}
}
class MyOwnException extends Exception {
public MyOwnException(String message) {
super(message);
}
}
Script.dsl:
method {
println a;
}
So the plan is that when I run the main() method in TestDelegate, it will run the DSL script, which calls for the method method(). Not finding it in the script, it will invoke methodMissing, which then invokes method() from myDelegate, which in turns invoke the closure, setting the delegate to the testDelegate. So far, so good. Then the closure is supposed to try printing out "a", which is not defined and will thus set off propertyMissing, which will will throw MyOwnException.
When I run the code, however, I get the following output:
Will throw error now!
Exception in thread "main" groovy.lang.MissingPropertyException: No such property: a for class: TestDelegate
Now, it must have reached that catch block, since it printed "Will throw error now!" It must have thrown MyOwnException too! But somewhere along the lines, MyOwnException was converted to MissingPropertyException, and I have no idea why. Does anyone have any idea?
P.S. if I remove closure.setResolveStrategy(Closure.DELEGATE_FIRST) from TestDelegate#method(), the code acts as expected and throws MyOwnException. But I really need the setResolveStrategy(Closure.DELEGATE_FIRST) for my DSL project. And I would prefer to know the root cause of this rather than just removing a line or two and see that it works without understanding why.
I think this is what essentially happens: With a delegate-first resolve strategy, the Groovy runtime first tries to access property a on myDelegate, which results in a MissingPropertyException because no such property exists. Then it tries propertyMissing, which causes a MyOwnException to be thrown. Eventually the runtime gives up and rethrows the first exception encountered (a design decision), which happens to be the MissingPropertyException.
With an owner-first resolve strategy, propertyMissing is consulted first, and hence MyOwnException is eventually rethrown.
Looking at the stack trace and source code underneath should provide more evidence.

Grails error handler always receives null pointer

We have a custom error controller that gets called after all of our errors. However, most of our errors that get thrown end up coming into the controller as null pointers, even though the original error was not a null pointer. Any ideas? Code below. Bootstrap and UrlMappings available if needed. Thanks
Error Handler method
def HandleErrors =
{
def exception = request.exception.cause.class
if (exception)
{
Exception ex = request.exception //This exception is always a NPE
...
Block of code throwing the exception. I originally did not have a try catch in here, but wanted to add it so that I was sure the exception being thrown was Not a NPE. Its a file not found exception.
try{
def writer = new FileWriter( new File(fileSaveLocation));
}
catch ( ex)
{
throw(ex)
}
Edit: Adding the exception that is pushed to the exception handler
Exception:org.codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException
Cause:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
It's not because you're referencing something that is null inside the error handler, and so are inadvertently throwing another exception, which is again caught?
can you try changing:
def exception = request.exception.cause.class
to
def exception = request?.exception?.cause?.class