Capturing error of entity.entityAspect.entityManager.createEntity - breeze

when I run below code -
this.entity.entityAspect.entityManager.createEntity('TblReceipt', {
ReceiptNo : 123
....
});
The code runs fine the first time and creates the new entity. Now, when the user tries to run it again its give below error -
Uncaught Error: A MergeStrategy of 'Disallowed' does not allow you to attach an entity when an entity with the same key is already attached"
The above error shows up in console. How I can catch this error in javascript code and show message to user?
Thanks

Yeah got it. Just wrap them in try catch block -
try {
}
Catch(error)
{
}

Related

throw Exception('oops!') vs throw 'oops!'

I notices some code that made me think the Exception function call was optional? E.g., do these two lines perform the same function?
throw Exception('oops!');
throw 'oops!'
No.
The former, throw Exception('oops!');, creates a new Exception object using the Exception constructor, then throws that object.
It can be caught by try { ... } on Exception catch (e) { ... }.
The latter, throw 'oops!'; throws the string object.
It can be caught by try { ... } on String catch (e) { ... }.
Generally, you shouldn't be doing either.
If someone made an error, something that would have been nice to catch at compile-time and reject the program, but which happens to not be that easy to detect, throw an Error (preferably some suitable subclass of Error).
Errors are not intended to be caught, but to make the program fail visibly. Some frameworks do catch errors and log them instead. They're typically able to restart the code which failed and carry on, without needing to understand why.
If your code hit some exceptional situation which the caller should be made aware of (and which prevents just continuing), throw a specific subclass of Exception, one which contains the information the caller needs to programmatically handle that situation. Document that the code throws this particular exception. It's really a different kind of return value more than it's an error report. Exceptions are intended to be caught and handled. Not handling an exception is, itself, an error (which is why it's OK for an uncaught exception to also make the program fail visibly).
If you're debugging, by all means throw "WAT!"; all you want. Just remove it before you release the code.

Guarantee that no error is thrown in Swift 3 do-catch?

If I have an asynchronous function that throws an error, how can I make sure no error is thrown before some more functions are executed?
do {
try foo()
} catch {
print("error: \(error)")
}
// HOW TO KNOW IF THE CALL WAS SUCCESSFUL HERE
I can have a variable that I change if an error is thrown, but it may be changed in some time if the call is asynchronous. I need to find a way to pause until foo() stops executing so I can be sure no error is thrown.

startVPNTunnel not throws errors

I'm calling startVPNTunnel() and then in the PacketTunnelProvider class, I execute the completionBlock with an error.
The call to startVPNTunnel() is inside a try-catch block, but it's seems that there is nothing to catch.
I just want to alert the user if the connection succeeded or not.
Anyone else was able to catch those errors?
The relevant code is very simple:
do {
try vpnManager!.connection.startVPNTunnel()
}
catch {
NSLog("roee: Failed to start vpn: \(error)")
}
And inside PacketTunnelProvider:
let error = NSError(domain: NEVPNErrorDomain, code: NEVPNError.ConfigurationInvalid.rawValue, userInfo: nil)
PacketTunnelProvider.pendingStartCompletionHandler!(error)
PacketTunnelProvider.pendingStartCompletionHandler = nil
I believe startVPNTunnel talks to OS and let it knows that VPN needs to be started.
OS will do couple of checks around VPN configuration and will immediately return positive result (if configuration is fine) or error (if something is wrong). And if an error is returned then this API will throw.
However, your Network extension will be started after startVPNTunnel returned.
So you can't communicate like that.
You can register for NSNotification.Name.NEVPNStatusDidChange to see VPN status changes.

Data not persisted after return from service

Let me preface this by saying my code was working yesterday. I rolled back my commits to the time when it was working.
The only thing that is different is that a migration was ran today to remove some columns from some tables. I can't see how this would affect it
I'm doing google oauth authentication and in my callback url from google I am doing a lot of saves/updates/etc.. My controller calls a single service that does everything. If I query the data while at a breakpoint where the return statement is, I can see the data. There are no exceptions, validation errors, or anything that would lead me to believe anything is wrong. Any ideas?
class MyController {
def myService
def callback() {
myService.update()
//At this point when I run X.get(1) it is returning null
redirect uri: "..."
}
}
#Transactional
class MyService {
def update() {
...
//If I break at this return statement I can run X.get(1) and it returns
return someData;
}
}
Edit: I've found the cause, however I don't understand how this is happening. I'm eventually calling userDetailsService.loadUserByUsername. That method is throwing a NoStackUsernameNotFoundException. I'm catching that exception in my code, however it is causing the transaction to roll back regardless.
Any exception thrown during a transaction, even if you catch it and deal with it, will cause the transaction to roll back.
To get around this you have a couple of options:
Perform a check before the point at which the exception is raised, and don't execute the offending code under conditions where you know it would throw an exception
Do the transaction handling yourself -
In your service, set
static transactional = false
Then declare your own transaction block:
MyDomain.withTransaction { tx ->
try {
userDetailsService.loadUserByUsername(...)
} catch (NoStackUsernameNotFoundException e) {
log.warn("Can't load user...")
}
//stuff you try to persist here will
//be written to the database successfully
//despite the NoStackUsernameNotFoundException
//being thrown & caught
}

Understanding exception handling mechanism (control flow) in Dart

Assume we have this Dart code:
void main() {
try {
try {
throw null;
} catch(e) {
throw null;
} finally {
print('first');
}
} finally {
print('second');
}
}
When running this code in browser via http://try.dartlang.org
Produced result very expected.
first
second
Uncaught Throw of null.
But if running this code in Dart VM then result very unexpected.
second
Unhandled exception: Throw of null.
This looks like first termination block (finally) never be executed.
I cannot understand this behavior of the exception handling mechanism.
Of course, we can assume that this is a bug.
But exception handling is the cornerstone of any system.
How Dart developers can explain this disparity?
P.S.
I think this question related to theme "about programming" and asking it here are the right place and time?
This appears to be an error in the VM, as far as I can tell. I've filed a bug:
https://code.google.com/p/dart/issues/detail?id=11758&thanks=11758&ts=1373396821
I should add that while the code is illustrative of the differing VM and dart2js implementations, it is a little error prone. If you feel that your catch block is going to throw, wrap the code inside the catch block within its own try-catch.
And, yes, I agree that this is a fine question to ask on StackOverflow. Thanks for posting this.

Resources