locked
show server side exception in javascript RRS feed

  • Question

  • On the server side, I throw exception as below.

            throw new ValidationException("The order is not paid yet.");

    On the client side, I show the error as 

        myapp.applyChanges().done(
            function success() {
                screen.refreshAll3Tabs();
            }
            , function failed(error) {
                msls.showMessageBox(error, { title: "Cannot Update Order Ready Status", buttons: msls.MessageBoxButtons.ok });
            });

    when I run the app, it shows error as follow

    How can I can show error message throw from server side exception?


    jl

    Tuesday, July 15, 2014 12:35 AM

Answers

  • You will want to use myapp.applyChanges().then rather than myapp.applyChanges().done.

    Then I think you want:

    msls.showMessageBox(error.message

    See:

    Deleting Data In The Visual Studio LightSwitch HTML Client


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com

    • Marked as answer by Joe Lee Tuesday, July 15, 2014 6:50 AM
    Tuesday, July 15, 2014 4:09 AM
  • I don't off hand, but you do the following:

    1. Go to the Server project

    2. Open the entity in question in the entity designer (ie. double click the entity in the solution explorer)

    3. Make sure [Server] (and not [HTMLClient]) is selected at the bottom of the entity designer screen

    4. Choose "Orders_Validate" from the "Write Code" menu at the top right of the entity designer screen

    5. You can then write code like the following in the editor and method that opens up (tweak logic as required):

    partial void Orders_Validate(AdvisorUserRating entity, EntitySetValidationResultsBuilder results)
    {
       if (entity.Status == (int)OrderStatus.Approved)
       {
           results.AddEntityError("Order cannot be approved at this time.");
       }
    }

    Hope this helps.


    Regards, Xander. My Blog


    • Edited by novascape Tuesday, July 15, 2014 7:01 AM
    • Marked as answer by Joe Lee Tuesday, July 15, 2014 7:03 AM
    Tuesday, July 15, 2014 7:01 AM

All replies

  • You will want to use myapp.applyChanges().then rather than myapp.applyChanges().done.

    Then I think you want:

    msls.showMessageBox(error.message

    See:

    Deleting Data In The Visual Studio LightSwitch HTML Client


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com

    • Marked as answer by Joe Lee Tuesday, July 15, 2014 6:50 AM
    Tuesday, July 15, 2014 4:09 AM
  • Just curious as to why you want to raise a server side validation exception and not handle the validation using the standard datasource validate methods on the server side? Doing it the standard way means that the validation error messages are propagated correctly to the client side without you having to do anything special.

    Regards, Xander. My Blog

    Tuesday, July 15, 2014 5:01 AM
  • Do you have a link that I can reference on how to use standard datasource validation method? thanks

    jl

    Tuesday, July 15, 2014 6:51 AM
  • I thought if I should always end the promise chain with done() and handle the error message there. Do you have any reason that I have to use then() instead of done() in this case?

    jl

    Tuesday, July 15, 2014 6:53 AM
  • I don't off hand, but you do the following:

    1. Go to the Server project

    2. Open the entity in question in the entity designer (ie. double click the entity in the solution explorer)

    3. Make sure [Server] (and not [HTMLClient]) is selected at the bottom of the entity designer screen

    4. Choose "Orders_Validate" from the "Write Code" menu at the top right of the entity designer screen

    5. You can then write code like the following in the editor and method that opens up (tweak logic as required):

    partial void Orders_Validate(AdvisorUserRating entity, EntitySetValidationResultsBuilder results)
    {
       if (entity.Status == (int)OrderStatus.Approved)
       {
           results.AddEntityError("Order cannot be approved at this time.");
       }
    }

    Hope this helps.


    Regards, Xander. My Blog


    • Edited by novascape Tuesday, July 15, 2014 7:01 AM
    • Marked as answer by Joe Lee Tuesday, July 15, 2014 7:03 AM
    Tuesday, July 15, 2014 7:01 AM
  • I thought if I should always end the promise chain with done() and handle the error message there. Do you have any reason that I have to use then() instead of done() in this case?

    jl

    I was told by the a member of the Microsoft team to use then :)

    Here is some documentation:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh700337.aspx

    There are a couple of differences between then and done

    • The then function returns a promise, but done  doesn't return a value. You can use then functions as intermediate functions in a chain (myPromise().then().then().then()), but done  must be the last function.

    • Unhandled exceptions in a then function are silently captured as part of the state of the promise, but unhandled exceptions in a done  function are thrown. Both functions can handle exceptions that have been passed to them as part of the state of a promise.


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com

    Tuesday, July 15, 2014 11:55 AM