locked
Can we return a custom error from Windows.Storage.StorageFile.createStreamedFileAsync?

    Question

  • It seems the only way to notify the caller of an error filling the stream is to call stream.failAndClose which results in an error continuation of "WinRTError: The system cannot find the file".

    I would like to have something more detailed (network response) as the error.

    return Windows.Storage.StorageFile.createStreamedFileAsync("" + serialNumber + ".txt", (function(stream) {
            return getAsync("car/" + serialNumber + expert).then((function(request) {
              var buffer;
              buffer = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(request.responseText, Windows.Security.Cryptography.BinaryStringEncoding.utf8);
              stream.writeAsync(buffer).then(function() {
                stream.close();
              });
            }), function(e) {
              stream.failAndClose(Windows.Storage.StreamedFileFailureMode.failed);
            });
          }), logoStream);

    Thursday, June 05, 2014 12:56 AM

All replies

  • Let me ask since I don't see it in the documentation - what does the value e have when the failure occurs?  My gut says that it contains the failure information that you want. I don't think there's any reason you can't store additional information somewhere rather than just calling failAndClose.

    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.

    Thursday, June 05, 2014 1:12 PM
    Moderator
  • If I don't call failAndClose, the caller of the StorageFile methods that need stream access will not get their error continuation being called. I even tried to re-throw "e" as well as returning an WinJS.Promise.wrapError(e) promise from the error handler.
    Thursday, June 05, 2014 9:32 PM
  • My point wasn't that you shouldn't call failAndClose, but that you can also store information in a var somewhere about the failure, then you call failAndClose.

    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.

    Thursday, June 12, 2014 5:32 PM
    Moderator
  • Matt, I do log the error to a logfile. But the caller that is using that failing StorageFile does not know what exactly happend in the error continuation. He just sees "WinRTError: The system cannot find the file". 

    I would like to provide the caller with a little bit more info. This kind of error reporting always bugged me in Windows. I know in the old Win32 API it was not easily possible to include the path/resource that it cannot find in the error message (altough IErrorInfo made that possible in the COM world, but was widely ignored). But with WinRT it should be possible for developers to compose *proper* error messages that actually carry vital informations about the error. In my case a bunch of server response headers.

    Any idea (from the Product Team) how this could be solved?

    Thursday, June 12, 2014 10:38 PM
  • So I have to ask: what more information is actually available at debug time? Anything that you would get at debug time should be available at runtime. If the information isn't there, then there's not much we can do.

    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.

    Monday, June 23, 2014 12:43 PM
    Moderator
  • I want to include the servers response in my error message. "The system cannot find the file" is not enough information I can display to the user. Its also plain wrong in most cases. Because in most cases, the server does not respond with 404 but 401, 500, 503... I want to display that to the user/caller of the streamed file methods.
    Monday, June 23, 2014 1:04 PM
  • I guess I am confused: It sounds like you're creating a file on a web server as opposed to the local machine.  Can you provide me with how this problem would occur so I understand better?

    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.

    Monday, June 23, 2014 3:22 PM
    Moderator
  • Matt, the code as I use it is in the first post of this thread. The "getAsync" call is an HttpClient.getString call internally.

    Lets assume the methods name, the code is copied from is: getCarData

    So I would call it like this:

    getCarData("23112321").then(function(file) {
      xml = new XmlDocument();
      xml.loadFromFileAsync(file)
    }, function (error) {
      console.error(error);
    });

    So my function returns an IStorageFile implementation. A delayed on. Its downloaded from the server only when somebody actually accesses the stream of the IStorageFile.

    So in case the resource does not exist on the server, guess what the console error method would print?

    "WinRT Error: The system cannot find the file specified".

    Now this message is utterly useless, because it omits the name/path of the "file" it cannot find and further information about the error. Why couldn't it find the file? Which paths did it search the file for, etc.

    And there seems to be no way to feed any more information to WinRT about the reason of the error. Much like the situation we had with WinRT Components until 8.1 where you could raise a Platform::Exception with a message string, but that message string was never handed over the ABI to the consumer on the JavaScript side. This eventually was implemented in 8.1 finally.

    So, my question/feature request is that:

    instead of this call (which produces the WinRT FILE_NOT_FOUND useless error)

    stream.failAndClose(Windows.Storage.StreamedFileFailureMode.failed);

    I'd like to have something like this:

    stream.failAndClose(Windows.Storage.StreamedFileFailureMode.failed, "Because the server returned 404 for this id:" + serialNumber);

    I hope its clear now?

    PS: The message editor tooling here sucks!



    • Edited by pkursawe Monday, June 23, 2014 3:36 PM
    Monday, June 23, 2014 3:35 PM