I am using a JavaScript plug-in and if there is an error it expects format like;
{error: 'You are not allowed to upload such a file.'}
In my MVC Web API I am throwing error like;
var error = string.Format("An error has been occured. Please try again later.");
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error));
and it is represented in HTTP response like below;
{"Message":"An error has been occured. Please try again later."}
how can I achieve to return in first way?
You can create an anonymous object
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError,new { error = "your error message here!"}));
Related
I have translated the 3D model and trying to view model using Autodesk Forge then am getting Token 500 error
When I ran code in debug mode I got the error message as Autodesk.Forge.Client.ApiException:'Error Calling Authenticate: Unable to connect to the remote server'"
TwoLeggedApi oauthApi = new TwoLeggedApi();
dynamic bearer = await oauthApi.AuthenticateAsync(
WebConfigurationManager.AppSettings["FORGE_CLIENT_ID"],
WebConfigurationManager.AppSettings["FORGE_CLIENT_SECRET"],
"client_credentials",
new Scope[] { Scope.BucketCreate, Scope.DataCreate, Scope.DataWrite, Scope.DataRead });
enter image description here
Entered the desired capabilities from appium desktop and started the seesion and got this error : "An unknown server-side error occurred while processing the command. Original error: Cannot read property 'replace' of undefined"
On SetUp should be ("platformName", "iOS")
public void StartDriver()
{
DesiredCapabilities cap = new DesiredCapabilities();
cap.SetCapability("platformName", "iOS");
cap.SetCapability("deviceName", "iPhone Xr");
cap.SetCapability("automationName", "XCUITest");
cap.SetCapability("app","YourApp.app");
cap.SetCapability("autoAcceptAlerts", true);
driver = new IOSDriver<IWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap, TimeSpan.FromSeconds(300));
Assert.IsNotNull(driver.Context);
}
It would be better if you could provide more information about your error, but guessing from the error text, this is caused when you do not provide platformName capability.
I am working with the Facebook graph API and have run into trouble regarding handling failed requests.
I use this code to create a new post
SocialFacebook.createPosting = function(data) {
var options = {
params: {
access_token : data.tokens.accessToken,
message : data.text
}
};
var url = 'https://graph.facebook.com/' + data.graphId + '/feed';
var response = HTTP.call('POST', url, options).data;
return response;
}
But instead of returning a JS object with error information in the response, it throws an error on failed requests
Exception while invoking method 'createPosting' Error: failed [500] {"error":{"message":"Duplicate status message","type":"FacebookApiException","code":506,"error_subcode":1455006,"is_transient":false,"error_user_title":"Duplicate Status Update","error_user_msg":"This status update is identical to the last one you posted. Try posting something different, or delete your previous update."}}
Because it's wrapped in an Error, the otherwise JSON object is now a string with some other stuff appended to it, which makes it difficult to parse and extract the attributes
Any idea as to why it throws an error, instead of returning a JS object with error details like usually?
Much appreciated
According to the docs, about HTTP.call():
On the server, this function can be run either synchronously or asynchronously. If the callback is omitted, it runs synchronously and the results are returned once the request completes successfully. If the request was not successful, an error is thrown.
So there you have it: since you called HTTP.call() synchronously (without providing a callback), if it responds with an error (in your case, Code 500) the error is thrown and not included in the data.
I have a site that is using Azure ACS for authentication, backed by ADFS. When things are going well and people do things they are supposed to its great but that doesn't happen always so we have been implementing custom error pages.
The problem is, it doesn't seem to catch authentication errors, such as
ID3206: A SignInResponse message may only redirect within the current web application
Key not valid for use in specified state.
These errors still produce the ugly yellow error screen no matter what I say in my web.config. They are clearly ASP.NET errors and not IIS errors, so my question is how and where can I put custom error pages to display such errors in a 'pretty' way, as setting a page in web.config isn't working?
EDIT: To be clear, we have ACS set up to use an error page, have customErrors on with a different error page, neither or being used.
You have to have an action on a controller in your web app that accepts a POST from ACS and takes a parameter of type string. You must also configure your relying party application in ACS to point to that action for errors. Then in the action code you can do something like this:
namespace ASPNETSimpleMVC.Controllers
{
public class ErrorController : Controller
{
// Errors can be mapped to custom strings here.
static Dictionary<string, string> ErrorCodeMapping = new Dictionary<string, string>();
static ErrorController()
{
ErrorCodeMapping["ACS50019"] = "You chose to cancel log-in to the identity provider.";
ErrorCodeMapping["ACS60001"] = "No output claims were generated. You may be unauthorized to visit this site.";
}
//
// POST: /Error/
//
// If an error occurs during sign-in, ACS will post JSON-encoded errors to this endpoint.
// This function displays the error details, mapping specific error codes to custom strings.
[AcceptVerbs( HttpVerbs.Post )]
public ActionResult Index( string ErrorDetails )
{
// The error details contain an array of errors with unique error codes to indicate what went wrong.
// Additionally, the error details contain a suggested HTTP return code, trace ID, and timestamp, which may be useful for logging purposes.
ErrorDetails parsedErrorDetails = new JavaScriptSerializer().Deserialize<ErrorDetails>( ErrorDetails );
ViewData["ErrorMessage"] = String.Format( "An error occurred during sign-in to {0}. ", parsedErrorDetails.identityProvider );
// Loop through all ACS errors, looking for ones that are mapped to custom strings.
// When a mapped error is found, stop looking and append the custom string to the error message.
foreach ( ErrorDetails.Error error in parsedErrorDetails.errors )
{
if ( ErrorCodeMapping.ContainsKey( error.errorCode ) )
{
ViewData["ErrorMessage"] += ErrorCodeMapping[error.errorCode];
break;
}
}
return View( "Error" );
}
}
}
You may also find this article helpful.
I have a simple login hub which clients may submit a username. The hub throws a exception if the username is already in use. On the client I am trying to handle the exception and read the message. However the message I am reading is 'One or more errors has occurred'.
try
{
await Client.SignIn(user);
UserName = SignInInput;
}
catch (Exception ex)
{ //One or more errors has occurred }
I asked this message on the SignalR github and I received the response:
You need to unwrap the errors. We provide this functionality via the SignalRError object. Here's a sample of how you can unwrap an Exception e.
using (var error = e.GetError())
{
Console.Error.WriteLine(error);
}
In the future please ask questions on sites such as Stackoverflow or chat with us in http://jabbr.net/#/rooms/signalr.
Now I'm looking through my Object Browser and I can't seem to find any such method or class anywhere.
It's an extension method, in SignalR.Client.ErrorExtensions.