[Dojo-interest] Best way in dojo to test whether a file exists via a url

248 views
Skip to first unread message

John Gerken

unread,
Oct 16, 2009, 7:47:59 AM10/16/09
to dojo-i...@mail.dojotoolkit.org
Hi gang,

I'm stuck on something I'd like to ask for help with please:

I have a need to test for the existance of a file accessible only by an out-of-domain URL and I'd like to be able to do this without sending errors to the client console when the file does not exist.  My current method of using a dojo.xhrGet to call a proxy, which in turn tries to get the file, seemingly doesn't allow me to do this because the error is posted to the console before I ever get control back in my error function or the deferred object.

Here's the structure of what I'm working with at the moment.  Obviously I'm scattershooting, but just so you can see.

                var url = <servlet proxy path> + <a url path> + <a file>;
                url = dojo.string.trim(url);

                // Create and hitch the callback functions
                var FileExistsCallback = dojo.hitch(this, function(response, ioArgs) {
                        console.debug("File exists");
                        <do stuff>
                        return response;                
                });
       
                var FileDoesNotExistCallback = dojo.hitch(this, function(response, ioArgs) {
                        console.debug("File does not exist");
                        <do stuff>
                        return response;
                });
                try {
                        var d = dojo.xhrGet({
                                url: url,
                                load: FileExistsCallback,
                                error: FileDoesNotExistCallback,
                                timeout: 60000,
                                sync: true,
                                handleAs: "text"
                        });
                } catch (e) {
                        // Whether successful or not, this code is never called
                        console.log("Caught my exception.  the exception is ", e);
                }

                d.addCallback(
                        function(result) {
                                console.log("In callback 1.  result is ", result);
                                return result;
                        }
                );
                d.addErrback(
                        function(result) {
                                console.log("In errback 1.  result is ", result);
                                return result;
                        }
                );

Of course when the file exists, all is right with the world.  But when it doesn't I get this posted to the console in Firebug:

GET http://10.211.55.3:8080/<proxy path>/<full file path> 404 Not Found 611ms dojo.js (line 16)






Error: Deferred Cancelled: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://10.211.55.3:8080/<path to dojo>/dojo/dojo.js :: anonymous :: line 16" data: no]
(function(){var _1=null;if((_1||(typeof ...setTimeout(dojo._loadInit,1000);}})();\r\ndojo.js (line 16)
Tech Preview not installeddojo.js (line 12095)

In errback 1. result is Error: Deferred Cancelled: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://10.211.55.3:8080/<path to dojo>/dojo/dojo.js :: anonymous :: line 16" data: no] dojoType=cancel

There has to be a better way to do this.  What'cha think?

Thanks,
John Gerken
Senior Architect, Emerging Internet Technologies
IBM Emerging Technologies Software Group
John_...@us.ibm.com
IBM Internal Blog:
http://blogs.tap.ibm.com/weblogs/john_...@us.ibm.com/
IBM External Blog:
http://www.ibm.com/developerworks/blogs/page/etech

Nathan Toone

unread,
Oct 16, 2009, 8:08:39 AM10/16/09
to dojo-i...@mail.dojotoolkit.org
If it's on another domain, you can't use xhrGet - you'll have to use dojo.io.iframe or something.

-Nathan

Jeff Ocs

unread,
Oct 16, 2009, 8:30:58 AM10/16/09
to dojo-i...@mail.dojotoolkit.org, dojo-i...@mail.dojotoolkit.org
An easy solution is to check for the file server side using php curl

Jeff

Sent from my iPhone

John Gerken

unread,
Oct 16, 2009, 8:41:16 AM10/16/09
to dojo-i...@mail.dojotoolkit.org
Hi Nathan,

Totally agree and understand.  That's why we've got the proxy in place, so it can handle the XD stuff for us.  That's also why I'm curious if there might be another alternative method to test the existance of a file accessable only via URL in a remote domain.  Any other way to do that that you can think of?  

Thanks for taking the time to answer.

John Gerken
Senior Architect, Emerging Internet Technologies
IBM Emerging Technologies Software Group
John_...@us.ibm.com
IBM Internal Blog:
http://blogs.tap.ibm.com/weblogs/john_...@us.ibm.com/
From: Nathan Toone <toon...@dojotoolkit.org>
To: dojo-i...@mail.dojotoolkit.org
Date: 10/16/2009 08:09 AM
Subject: Re: [Dojo-interest] Best way in dojo to test whether a file exists        via a url


Nathan Toone

unread,
Oct 16, 2009, 8:58:46 AM10/16/09
to dojo-i...@mail.dojotoolkit.org
Ah - I mis-understood your question.  You are just wondering about suppressing the error message.

There is a new flag, in 1.4, that will allow for this - set failOK to true.

http://bugs.dojotoolkit.org/ticket/9618
http://docs.dojocampus.org/dojo/xhrGet#dojo-xhrget-supported-object-properties

However, it's not *that* big of a deal to have the error message show up...it would only show up for those looking at the console in firebug or IE...

-Nathan

Dustin Machi

unread,
Oct 16, 2009, 8:59:56 AM10/16/09
to dojo-i...@mail.dojotoolkit.org
John,

I think you are doing it correctly other than you shouldn't have the
try/catch around the xhrGet. The Deferred object that returns handles
trapping errors that occur within. The issue here I don't believe is
your code, but rather FF/Firebug. Tom Trenka recently asked me a
similar question to see if I was getting the same behavior, and we
found that FB displays the 401 errors on xhr requests regardless of
whether the error was trapped or not. I believe, at least at the
time, we were unable to turn that error message off without turning
off Firebug. They (firebug devs) may have changed this now so that
you can toggle the display of those errors.

Thanks,
Dustin


On Oct 16, 2009, at 8:41 AM, John Gerken wrote:

> Hi Nathan,
>
> Totally agree and understand. That's why we've got the proxy in
> place, so it can handle the XD stuff for us. That's also why I'm
> curious if there might be another alternative method to test the
> existance of a file accessable only via URL in a remote domain. Any
> other way to do that that you can think of?
>
> Thanks for taking the time to answer.
> John Gerken
> Senior Architect, Emerging Internet Technologies
> IBM Emerging Technologies Software Group
> John_...@us.ibm.com
> IBM Internal Blog: http://blogs.tap.ibm.com/weblogs/john_...@us.ibm.com/
> IBM External Blog: http://www.ibm.com/developerworks/blogs/page/etech
>
>
> From:
> Nathan Toone <toon...@dojotoolkit.org>
> To:
> dojo-i...@mail.dojotoolkit.org
> Date:
> 10/16/2009 08:09 AM
> Subject:
> Re: [Dojo-interest] Best way in dojo to test whether a file
> exists via a url
>
>
>
>

> <smime.p7s>_______________________________________________

John Gerken

unread,
Oct 16, 2009, 9:09:23 AM10/16/09
to dojo-i...@mail.dojotoolkit.org
Fascinating Dustin.  Perhaps that's what I'm seeing here too.  I need to get a handle on how critical (or not) the display of that message in the console is to the architect in charge.  Thanks for your insight.

Thanks,
John Gerken
Senior Architect, Emerging Internet Technologies
IBM Emerging Technologies Software Group
John_...@us.ibm.com
IBM Internal Blog:
http://blogs.tap.ibm.com/weblogs/john_...@us.ibm.com/
IBM External Blog:
http://www.ibm.com/developerworks/blogs/page/etech


From: Dustin Machi <dma...@dojotoolkit.org>
To: dojo-i...@mail.dojotoolkit.org
Date: 10/16/2009 09:00 AM
Subject: Re: [Dojo-interest] Best way in dojo to test whether a file        exists        via a url

John Gerken

unread,
Oct 16, 2009, 9:16:10 AM10/16/09
to dojo-i...@mail.dojotoolkit.org
Yea, suppressing the error message is the primary issue, but unfortunately I can't move to 1.4 either.  You're right about the severity issue.  I need to take that up with the architect in charge.  But beyond that, after reading a bit, you may still have a good potential alternative with dojo.io.iframe.  If the proxy I'm using is the problem (and not as Dustin suggets, just FB) then io.iframe may get me around the proxy.  So I'm going to give that a shot and see what I come up with.

One thing I've not been able to figure out yet that you might have an idea on:  The deferred object has a silentlyCancel attribute and also has a state indicator of some type.  But it's unclear to me how I'd manipulate those early enough in the code to prevent the error display on the console.  The first two errors are displayed before my FileDoesNotExistCallback function is called, so I can't do it there.  Is it possible to manipulate that deferred object in some way earlier in the process?  What about doing something hackish like subclassing deferred or something totally obnoxious like that?  ;-)

Thanks,
John Gerken
Senior Architect, Emerging Internet Technologies
IBM Emerging Technologies Software Group
John_...@us.ibm.com
IBM Internal Blog:
http://blogs.tap.ibm.com/weblogs/john_...@us.ibm.com/

Dustin Machi

unread,
Oct 16, 2009, 9:52:40 AM10/16/09
to dojo-i...@mail.dojotoolkit.org

On Oct 16, 2009, at 9:16 AM, John Gerken wrote:

> Yea, suppressing the error message is the primary issue, but
> unfortunately I can't move to 1.4 either. You're right about the
> severity issue. I need to take that up with the architect in
> charge. But beyond that, after reading a bit, you may still have a
> good potential alternative with dojo.io.iframe. If the proxy I'm
> using is the problem (and not as Dustin suggets, just FB) then
> io.iframe may get me around the proxy. So I'm going to give that a
> shot and see what I come up with.

The script io will get you around the proxy fairly easily, though it
won't solve the actual issue at hand regarding the logging. There are
a couple other xd techniques as well. Kris Zyp had a few good
articles on the sitepen blog regarding some of these techniques as
well as the xhrPluginRegistry.

> One thing I've not been able to figure out yet that you might have
> an idea on: The deferred object has a silentlyCancel attribute and
> also has a state indicator of some type. But it's unclear to me how
> I'd manipulate those early enough in the code to prevent the error
> display on the console. The first two errors are displayed before
> my FileDoesNotExistCallback function is called, so I can't do it
> there. Is it possible to manipulate that deferred object in some
> way earlier in the process? What about doing something hackish like
> subclassing deferred or something totally obnoxious like that? ;-)
>

silentlyCancel is a different attribute that controls whether or not
the error handler will be triggered if you cancel a deferred, I don't
think it is going to help you much here. Note that the order of
attaching deferreds has an impact on how/when they get called. This
post, http://www.sitepen.com/blog/2009/03/31/queued-demystifying-deferreds/
, demonstrates how the order of attaching affects the behavior.

Thanks,
Dustin

> Thanks,
> John Gerken
> Senior Architect, Emerging Internet Technologies
> IBM Emerging Technologies Software Group
> John_...@us.ibm.com
> IBM Internal Blog: http://blogs.tap.ibm.com/weblogs/john_...@us.ibm.com/
> IBM External Blog: http://www.ibm.com/developerworks/blogs/page/etech
>
>
> From:
> Nathan Toone <toon...@dojotoolkit.org>
> To:
> dojo-i...@mail.dojotoolkit.org
> Date:
> 10/16/2009 08:59 AM
> Subject:
> Re: [Dojo-interest] Best way in dojo to test whether a file
> exists via a url
>
>
>
>

Reply all
Reply to author
Forward
0 new messages