how to get the Compiler error message? asp.net - c#

How can I get the Compiler Error Message if I have the pageUrl?
I tried using the HttpWebRequest class, but I haven't gotten the result yet.
I have collection of pages, that must execute automatically, and if the page fails, I need it to create a log.
Thank you

You can catch all application errors in application global class (global.asax) in Application_Error handler.
Other way. You can catch exceptions in custom error module as well, just register you module in <httpModules> section and implement following function there:
void context_Error(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
Exception ex = context.Server.GetLastError();
//... here goes some code
}
Thus you have to ways to catch any error. Other task is to request all pages. As I can see from your post, you've already have such solution.

string pagetext = (new System.Net.WebClient()).DownLoadString(<url>);
//Add a better control here
if(pagetext.Contains("Server Error"))
{
`enter code here`
}

You can write a program to visit the pages, if the response of the request is a HTTP error than you can investigate further.
If you do not want to write your own program to detect errors originating from HTTP requests, you can use a testing framework like selenium.

Disclaimer: I do very little with ASP.NET type stuff.
ELMAH might help you a bit. It's an error logger for ASP.NET projects.

Where are you trying to catch these exceptions? In your website, or in an external application that is crawling a site?
In an external application, using an HttpWebRequest you'd do something like this:
string urlToTry = "http://www.example.com/ServerErrorPage.htm";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToTry);
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Process your success response.
}
catch (WebException we)
{
HttpWebResponse error = (HttpWebResponse)we.Response;
// If you want to log multiple codes, prefer a switch statement.
if (error.StatusCode == HttpStatusCode.InternalServerError)
{
// This is your 500 internal server error, perform logging.
}
}
The WebException class will give you messages like:
The remote server returned an error: (500) Internal Server Error.
Once you cast them to an HttpWebResponse, you can get at the StatusCode and perform whatever logging you require.

Related

Whats the correct HTTP status code answer for specific Exceptions

I'm creating a REST Web API in C# using ASP.NET Web API for MVC 5.
I've created an abstract class BaseApiController, which all API controllers extend. In this base controller I handle all the Exceptions and everything the controllers need to properly work.
I've already implemented exception handling for the following Exceptions:
public override async Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
{
string controllerName = null;
string actionName = null;
try
{
SetContext(controllerContext);
SetServices();
await AuthenticateUser();
controllerName = controllerContext?.Controller?.GetType().FullName;
var services = controllerContext?.ControllerDescriptor?.Configuration?.Services;
actionName = services?.GetActionSelector()?.SelectAction(controllerContext)?.ActionName;
return await base.ExecuteAsync(controllerContext, cancellationToken);
}
catch (HttpResponseException e)
{
ClientDataService.Logger(e, $"{controllerName}.{actionName}",
$"Response -> Status Code: {(int)e.Response.StatusCode}, Reason: {e.Response.ReasonPhrase}",
SystemLogOriginTypesEnum.WEBSITE_API);
throw;
}
catch (Exception e)
{
ClientDataService.Logger(e, $"{controllerName}.{actionName}",
$"Response -> Status Code: {(int)HttpStatusCode.InternalServerError}, Reason: Internal server error",
SystemLogOriginTypesEnum.WEBSITE_API);
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("Internal server error"),
ReasonPhrase = "Internal server error"
});
}
The Controllers throw HttpResponseException mainly when mandatory parameters aren't passed or properly setup (HTTP status code
400), the second is when something went terribly wrong (HTTP status code 500).
My question is what kind of HTTP status code should the API respond when a service throws exceptions like a NotSupportedException or a custom InvalidSortAttributeException?
I don't mean HTTP status categories, i.e., 4XX vs 5XX, since the InvalidSortAttributeException should be a 4XX category because the error gets thrown from an invalid request, maybe a 400 (Bad Request)!?
Although the second, I believe could fall on both categories since the server doesn't support the request but it doesn't exactly mean it will not support it in the future, maybe a 406 (NotAcceptable) or a 501 (NotImplemented)?
The 4XX is built specifically for user errors
The 5XX is built for internal server errors
You have to respond with an intelligent code for the scenario. If there is a notsupported exception but your code caller constructed a not supported scenario, then it's 500. If the user managed to put together a bad request, it should be 4XX.
Note also that there's a difference between missing an attribute in the JSON request (400) and missing a value in the url (404) or the resource is no longer available (404).

.NET throws WebException instead of setting StatusCode

I have the following code in my method:
// make the FTP request
var request = (FtpWebRequest)WebRequest.Create(serverUri);
request.KeepAlive = true;
request.Method = WebRequestMethods.Ftp.MakeDirectory;
return (FtpWebResponse)request.GetResponse();
The serverUri is valid and this works if the directory does not exist already on the server. However, if the directory does already exist a System.Net.WebException : The remote server returned an error: (550) File unavailable (e.g., file not found, no access) occurs.
Why does this exception occur when I call request.GetResponse() instead of setting the FtpWebResponse object's StatusCode to FtpStatusCode.ActionNotTakenFileUnavailable which is 550?
I find information on how to handle the exception and get the status code and description from it. But I would like to know why the exception is thrown in the first place instead of setting the response object's status code and letting the coder decide if it warrants an exception.
Because getting an error code after making an Http request matches perfectly with the Exception semantics of the language. You don't actually have a meaningful response to get back and since it is an "exceptional" case it makes sense to translate the web error to a WebException.
This is actually behaving as designed. You'll need to catch that specific exception and then interrogate the response. Per the MSDN documentation:
If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.
So, the code might look like this:
try
{
return (FtpWebResponse)request.GetResponse();
}
catch (WebException we)
{
// interrogate the WebException for the response data you're looking for
}

Setting the response error in authentication handler

I'm writing a Web API in using the MVC 4 Web API framework. I'm using BASIC authentication with a custom authentication message handler inheriting from DelegatingHandler.
Everything is working peachy, except my error handling.
I'm using the Web API as a wrapper over an existing authentication mechanism and an exception is thrown when the user's password is expired. Within the API action methods, I am able to handle this easily by throwing an HTTPResponseException and I get a tidy little json object such as below:
{
"Message": "Password is expired."
}
If I try this same approach in my Auth Message Handler, I get all the nasty .NET response output.
<html>
<head>
<title>Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
... you get the idea ...
I think I'm close... in SendAsync
catch (Exception ex)
{
var response = request.CreateErrorResponse(HttpStatusCode.Unauthorized, ex.Message);
response.Headers.Add("WWW-Authenticate", "Basic realm=\"myrealm\"");
}
Here, perhaps if I can somehow get a Task<HttpResponseMessage> from my response object to return, I think I might be ok.
Basically my question is "How should exception handling really be done so that I am able to return a nice json/xml object to the user?"
One more thing to note, this is .NET 4, not 4.5. I've seen a few examples using the await keyword, but that won't work in this scenario.
You can wrap your response in a task:
catch (Exception)
{
return Task<HttpResponseMessage>.Factory.StartNew(() =>
{
var response = request.CreateErrorResponse(HttpStatusCode.Unauthorized, ex.Message);
response.Headers.Add("WWW-Authenticate", "Basic realm=\"myrealm\"");
return response;
});
}
return base.SendAsync(request, cancellationToken);

How to completely disable response body (ResponseStatus) for uncaught exceptions in ServiceStack, but keep StatusCode

Information of what I have and what I am trying to achieve
I am using ServiceStack and have it up and running for what I need, however I am unable to find out how to disable the Body/Content for uncaught exceptions.
I have ServiceStack handling ALL routes.
If I navigate to a route which is not mapped to ServiceStack, I get a StatusCode of 404 (perfect) and content in the Body of the response of "Handler for Request not found: ...."
If my code throws an uncaught exception, ServiceStack will kindly return a relevant StatusCode, however it also returns a ResponseStatus with ErrorCode and Message populated.
I have DebugMode turned off, this disables the StackTrace, however I want to completely mute the entire Body of the response for exceptions.
What I have tried
I have tried a Response Filter of the following:
ResponseFilters.Add((req, res, dto)) =>
{
if (dto is Exception) res.Close();
});
it unfortunately did not work.
What I want to avoid
try{
return service.GetResponse();
} catch (Exception) {
return new HttpResult(.....);
}
My Question
How do I disable the response body for all uncaught exceptions, but still return the StatusCode? I would like null returned in the body, and StatusCodes to remain in tact.
I've tried to make my question clear, but if I have been a bit vague in any way, please ask me questions.
You should be able to override the default exception handling behavior by overriding HandleException of ServiceBase.
You can inspect the default implementation here: https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.ServiceInterface/ServiceBase.cs#L228.

How to process http links?

As you all know there is many file host websites, is there a way to process the http link of a of file on one of those sites and retrieve a result if the file exists or if the http link even exists or not. I know that maybe some of those file host websites uses their own APIs but i want a more generic way.
Edit:
So as i understand there is no file on a server, it's just that i have to read the response and read it properly. I want to ask another thing, what about redirection, does that mean if i got the response of a link that redirects to other link, i will get the final target from the response ?
You can find out if a file exist using the exists method:
bool System.IO.File.Exists(string path)
///
in order to find out if a file exist on a remove server you can try this:
WebRequest request;
WebResponse response;
String strMSG = string.Empty;
request = WebRequest.Create(new Uri(“http://www.yoururl.com/yourfile.jpg”));
request.Method = “HEAD”;
try
{
response = request.GetResponse();
strMSG = string.Format(“{0} {1}”, response.ContentLength, response.ContentType);
}
catch (Exception ex)
{
//In case of File not Exist Server return the (404) Error
strMSG = ex.Message;
}
see this:
If I understand you correctly, you're trying to tell if a given URL has content.
Use the
WebClient
class.
Call the url, if you receive a 200, you're good to go. A 404 exception or similar probably means the link is no good.
Or, even better way to do this is to do a HEAD http request. See here for more info on that.

Categories

Resources