When you’re building a PAC file or making a major modification to one, chances are you won’t get it right the first time – You’ll miss a closing brace, a semi-colon, or something just won’t be right. Unfortunately, most browsers provide no error handling for PAC files – It just won’t load and everything will go browser direct.
The important thing to remember about troubleshooting a PAC file is that it is a totally iterative process for each URL. The browser will process the PAC file from the top until it finds an error or until it finds a valid return.
If your PAC is erroring out, use alert() (described above) to throw in lots of pop-ups to help you find your error. If you throw in four alerts in your PAC and you only get three of showing up, you know your error is somewhere between alert #3 and alert #4. Keep using this to narrow it down and you’ll soon find your problem. It’s a pain, but there’s no other good way to do it. Needless to say, you should do troubleshooting and testing on a PAC file that’s NOT in production.
The day will come when you are trying to troubleshoot a PAC file and NOTHING works – NO alerts pop up, nothing, nada. Hair will be pulled. Co-workers will be yelled at. A bad day will be had by all. This has happene to me, it will happen to you. Before you get to extreme, do yourself a favor – Check the settings in your browser. Did you typo something? Can you copy-and-paste the exact URL show in the autoconfig URL into the browser address bar and get back a file? This sounds strange, but it happens. Get it in your head to double-check your settings.
Obviously, PAC files require a lot of testing and tuning. Some people might not have a handy HTTP server to load them onto for testing. If that’s the case, or if you want to edit them on your local computer it’s relatively simple to load the PAC file from your local hard drive.
Simply copy the PAC file as a simple text file somewhere onto your hard drive. In this case, I loaded mine into a file called “C:\PAC\TEST.PAC”. To get IE to recognize it as a PAC file you need to enter it with forward slashes in the path, not back slashes, and use the file:// prefix, not http:// as you would loading it from a web server. Here’s the URL for IE to load my PAC file from my hard drive - file://c:/pac/test.pac. I would, however, strongly NOT recommend using this notation for a production PAC file. Even if you don’t expect to update it frequently it’s still better to use a central PAC file hosted on a web server.
Google has produced an incredible tool called the PAC Tester. This application will parse your PAC files and find the errors for you. You can specify the source IP address, to test those isInNet calls and the URL to be queried. A link to it can be found in the References and Tools menu of this site. I also took the liberty of writing a quick web front end to the application - This allows you to just copy-and-paste your PAC file into the page and specify the client IP to be simulated and the URL and it will run it through the tester. It's ugly, but functional.
One note on the PAC tester - I've seen some odd behavior when I test PAC files using alert() on the Windows version. Seems to work well on other variants, including the one that runs my web front end, so try your PAC file there and see if it works.
I received a great tip from Pasquale Scopelliti on troubleshooting syntax errors that cause a PAC file not to load. You can wrap your PAC file into a HTML document and load it via your browser. If it’s HTML and the PAC has a syntax error it will report it to you and tell you what’s wrong. Here’s how to do so.
Create a basic HTML file on a web server that contains the following:
<html>
<head>
<script language="JavaScript">
Insert your ENTIRE PAC file here
</script>
</head>
<body>
test
</body>
</html>
Save the file to a web server or your local hard drive and then load it up in Internet Explorer. You’ll just see the text “test” in the main screen of the browser, but the real action happens on the error bar – Look in the very bottom-left corner of the window for a yellow triangle with a black explanation point on top of a piece of paper. Double-click on this icon and then choose “Show details”. This displays the error message in question. Note that the line # is from the top of the HTML file, not from your PAC file. It won’t always give you the exact details, but it’s a good way to help dig into those hard-to-find syntax errors.
There are a number of Online JavaScript debuggers that you can use. They will allow you to copy/paste your PAC file into their site and it will check the syntax and let you know if there are any issues that need to be fixed. One recommended by our users is JSLint - www.jslint.com/