0
votes

How to get Sql Server database trowed errors details at client side (like sql raiserror level) using Breeze manager perhaps in saveChanges fail function. For example :

// SERVER-SIDE (SQL-SERVER : TRIGGER - After Insert)

    RAISERROR ('Espace utilise par Place, Cannot delete.', 16, 1)
    ROLLBACK TRAN
    RETURN

// CLIENT-SIDE (BREEZE : saveChanges - saveFailed)    
        var saveChanges = function () {
          .....
          function saveFailed(error) {
                var msg = 'Save failed: ' + getErrorMessages(error);
                logError(msg, error);
                error.message = msg;
                throw error;
          }
     };

UPDATE 1 : Ok, after updated my breeze version to 1.4.1 I've got the error details at client but :

a) application stop at server-side (Breeze controller Api) with InvalidOperationException at code bellow point without any debug breakpoint assigned.

 [HttpPost]
 public SaveResult SaveChanges(JObject saveBundle)
 {
   return _contextProvider.SaveChanges(saveBundle);
 }

b) if I force it to continue, I get the errors at client side at Breeze saveChanges saveFailed level. How to manage this error to bypass Breeze server-side api controller error handler but continue to have related infos at client?

In waiting for an adequate answer I'm trying to reported these business rules at BeforeSaveEntities scope. But it will be painful to prohibit all use of all triggers rules in lieu of BeforeSaveEntities : they don't have same functions.

1

1 Answers

0
votes

Did you consider re-implementing your SaveChanges Web API method with a try/catch?

You should be able to intercept the exception, analyze it, and change it into whatever response you think is suitable for the client.

Update Aug 21, 2013

I want to be clear about your question and my answer. If you do nothing and there is an uncaught exception in your Web API method, Breeze.NET on the server should forward that error and BreezeJS will forward it to your saveChanges fail handler. That conforms to your experience, correct?

I assumed that you wanted to intercept the exception ON THE SERVER and perhaps re-shape the exception for the client or compose your own (failing) response to the client. For this purpose I recommended the try/catch.

What kind of trouble are you having "trying to decorate server side saveChanges with Try/Catch"? I thought that should catch exceptions thrown by SQL server ... but perhaps not. The execution might take place outside of the Web API method, within the scope of the [BreezeControllerAttribute] (which delegates to a [BreezeQueryableAttribute] which wraps the Web API [QueryableAttribute]).

Is that the problem now?

Upon further reflection, a better place for try/catch may be in an override of the SaveChangesCore method in a custom (derived) implementation of the EFContextProvider:

public class YourCustomProvider : EFContextProvider<YourDbContext>
{
    protected override void SaveChangesCore(SaveWorkState saveWorkState)
    {
        try
        {
            base.SaveChangesCore(saveWorkState);
        }
        catch (System.Exception ex)
        {
            // whatever
        }       
    }
}

If that doesn't catch it, then the breakdown is much harder to reach and you would have made a good argument for Breeze.NET to provide an exception handling hook in the scope of the [BreezeQueryableAttribute].

As a workaround you might have to re-implement the [BreezeControllerAttribute] (it is open source) to provide that hook for yourself. If you do ... and it works well ... please send it to us (clone/fork/pull request) so other may benefit. We'll also investigate this independently when we have a moment.