views:

18

answers:

1

I'm currently writing a PHP script that 3rd party clients will be able to access using GET. It will take a few GET parameters and return an XML document with results.

My question is, if required GET parameters are left out or contain invalid data how should display errors back to the end user?

Should I return a completely different XML document with the error details or the same XML document with errors in the existing fields or just put the errors as HTML?

+3  A: 

How is the content requested? Do some users surf to the page? Or is another PHP script requesting those details? If it's a PHP script requesting the XML, I would just output HTTP 400 Bad Request. If it's a human being browsing to the site, I think a friendly error page would be fine if the parameters are crucial.

For example, if they are requesting for

xml.php?file=report1584.xml

but they did not supply the file parameter, you would simply tell them that there was an error, no file was specified.

Kai Sellgren
It will be used by other scripts and possibly viewed by humans too and will probably be parsed by a DOM parser. My worry is that if I return a HTML error message then it will not be XML and will break if a parser tries to read it.
Camsoft
Whatever you do, you should tell other developers who your system returns/informs about errors. Take a look at how Facebook shows errors in XML form: http://wiki.developers.facebook.com/index.php/Error_codes
Kai Sellgren
That's helpful. So is it best practice to always return an XML document?
Camsoft
I can't think of a problem in it. If there's a script parsing the XML, surely it would be able to parse the errors, too, like in case of Facebook's architecture. If it's a human being requesting the XML, he would notice it, too, especially if he's using an XML reader. The system would also be more consistent, it would always return an XML document.
Kai Sellgren