locked
Throw new Error or WinJS.Promise.wrapError() - what's recommended? RRS feed

  • Question

  • What are the implications of both? Does it make any difference if I re-throw an error from within a Promises error handler or return a WinJS.Promise.wrapError from the error handler?
    Tuesday, November 27, 2012 1:59 PM

Answers

  • It depends on what you need to do with the promise.  Docs say this is the point of wrapError:

    Wraps a non-promise error value in a promise. You can use this function if you need to pass an error to a function that requires a promise.

    If you need to pass an error to a function that requires a promise, then that's what you need.  If you're handling the error otherwise, then you don't need this function. 


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, March 22, 2013 6:48 PM
    Moderator

All replies

  • Still haven't figured this one out. Should we use wrapError in code like this:

    return doo.data.withDatabase(function(db) {
          return db.oneAsync("SELECT something from a table WHERE path=?", [path]).then(function(result) {
            if (result) {
              futureAccessList.remove(result.token);
              return db.runAsync("DELETE FROM WatchedFolders WHERE id=?", [result.id]);
            }
          });
        })
    At the point where result is checked, should I use wrapError(new Error("Not found")) or just throw new Error("Not found")?

    Tuesday, March 19, 2013 11:05 AM
  • It depends on what you need to do with the promise.  Docs say this is the point of wrapError:

    Wraps a non-promise error value in a promise. You can use this function if you need to pass an error to a function that requires a promise.

    If you need to pass an error to a function that requires a promise, then that's what you need.  If you're handling the error otherwise, then you don't need this function. 


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, March 22, 2013 6:48 PM
    Moderator