I want to throw an exception if a web page that TuesPechkin is generating for PDF returns HTTP Error 404 or 500. I don't want to get a PDF file for the friendly-looking HTML error page.
It have tried the EventHandler (see the codes below), but none of them capture the error code or error message. The only time ErrorEvent is raised is when I pass in a URL with bogus domain name, like http://www.google.bogus
Pechkin has class property called 'HttpErrorCode' in SynchronizedPechkin. Where is it in TuesPechkin?
public ActionResult Index()
{
var messages = new StringBuilder();
IConverter converter =
new ThreadSafeConverter(
new RemotingToolset<PdfToolset>(
new Win64EmbeddedDeployment(
new TempFolderDeployment())));
var obj = new ObjectSettings();
obj.PageUrl = "http://www.google.com/dummy";
var doc = new HtmlToPdfDocument();
doc.Objects.Add(obj);
converter.Error += new EventHandler<ErrorEventArgs>(converter_Error);
converter.Warning += new EventHandler<WarningEventArgs>(converter_Warning);
converter.PhaseChange += new EventHandler<PhaseChangeEventArgs>(converter_PhaseChange);
converter.Begin += new EventHandler<BeginEventArgs>(converter_Begin);
byte[] pdfBinary = null;
try
{
pdfBinary = converter.Convert(doc);
}
catch (Exception e)
{
messages.AppendLine(e.Message);
}
finally
{
converter.Error -= new EventHandler<ErrorEventArgs>(converter_Error);
converter.Warning -= new EventHandler<WarningEventArgs>(converter_Warning);
converter.PhaseChange -= new EventHandler<PhaseChangeEventArgs>(converter_PhaseChange);
converter.Begin -= new EventHandler<BeginEventArgs>(converter_Begin);
}
if (messages.Length > 0)
{
throw new ApplicationException(messages.ToString());
}
return File(pdfBinary, "application/pdf");
}
void converter_Begin(object send, BeginEventArgs e)
{
messages.AppendLine(e.ToString());
}
void converter_PhaseChange(object send, PhaseChangeEventArgs e)
{
messages.AppendLine(e.PhaseDescription);
}
void converter_Warning(object send, WarningEventArgs e)
{
messages.AppendLine(e.WarningMessage);
}
void converter_Error(object sender, ErrorEventArgs e)
{
messages.AppendLine(e.ErrorMessage);
}