Unable to see response body from HTTPWebRequest when status is 400 - windows-phone-7

In the following, ar.GetResponseAsString() just throws an exception.
Here is my code:
AsyncCallback GetTheResponse = ar =>
{
try
{
var result = ar.GetResponseAsString();
Debug.WriteLine(string.Format("ResponseStream:::{0}", result));
callback(result, null);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
What am I doing wrong?

Catch the WebException that the 400 is throwing then grab the response from the exception message. That's where your error response is hiding

Related

How to handle feign client connection timeout

I have below code to check this error but I am not getting timeout error its going to else condition
Response response = null;
try {
response = client.getResponse(URI.create(uri), headers, reuest);
} catch (Exception ex) {
if(ex instanceof SocketTimeoutException){
throw new ExternalClientException(Errors.TIMEOUT_ERROR);
} else {
throw new ExternalClientException(Errors.UNEXPECTED_ERROR);
}
}
You need to catch feign.RetryableException instead of SocketTimeoutException.
javadoc
Please send all code of class. You need to provide class of "client" variable so people can help.
One more thing, instead of checking instance of exception in "catch" clause, you should use multiple catching like this:
Response response = null;
try {
response = client.getResponse(URI.create(uri), headers, reuest);
} catch (SocketTimeoutException ex1) {
throw new ExternalClientException(Errors.TIMEOUT_ERROR, ex1);
} catch (Exception ex2) {
throw new ExternalClientException(Errors.UNEXPECTED_ERROR, ex2);
}

Unable to handle Exception properly in spring boot pubsub app

This is my code where i have problem:
try {
publisher.publish(payload).get();
} catch (com.google.api.gax.rpc.DeadlineExceededException e) {
LOGGER.error("com.google.api.gax.rpc.DeadlineExceededException occured: " + e.getCause());
} catch (com.google.api.gax.rpc.NotFoundException e) {
LOGGER.error("com.google.api.gax.rpc.NotFoundException occured: " + e.getCause());
} catch (com.google.api.gax.rpc.ApiException e) {
LOGGER.error("com.google.api.gax.rpc.ApiException occured: " + e.getCause());
} catch (io.grpc.StatusRuntimeException e) {
LOGGER.error("io.grpc.StatusRuntimeException occured: " + e.getCause());
} catch (java.lang.RuntimeException e) {
LOGGER.error("RuntimeException occured: " + e.getCause());
} catch (java.util.concurrent.ExecutionException e) {
LOGGER.error("java.util.concurrent.ExecutionException occured: " + e.getCause()); // my program cursor always come to this point
} catch (Exception e) {
LOGGER.error("Exception occured: " + e.getCause());
}
And the getCause of ExecutionException catch is:
java.util.concurrent.ExecutionException occured:
com.google.api.gax.rpc.NotFoundException:
io.grpc.StatusRuntimeException: NOT_FOUND: Resource not found
If you see I already put the catch for "com.google.api.gax.rpc.NotFoundException" in second catch, so why it went into the ExcutionException catch.
Due to this nature I am not able to write proper message for the client to address.
Thanks in advance if anyone can help.
When publisher.publish(payload).get() would have been executed it would have thrown java.util.concurrent.ExecutionException ex= new com.google.api.gax.rpc.NotFoundException() i.e reference of java.util.concurrent.ExecutionException but object of com.google.api.gax.rpc.NotFoundException. So in try catch , when reference was checking which catch block to go, it could not get through api.gax.rpc.NotFoundException() as cactch only catches same or super class so it got catch later but since object was madeup of com.google.api.gax.rpc.NotFoundException so getCause got overridden and when you executed it object method was called.
Resolution: eg
try{
}
catch(Exception e){
throw (e.getClass()).cast(e);
}
where you can put this catch block in java.util.concurrent.ExecutionException.

How to fix this exception type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in

This is continuation to my question: [How to get name data of multiple json object list that will be posted to web api?
I was able to update my code but now I am getting an erexception on
if (db.Sales1.Any(sl => sl.ExtSerial != s.ExtSerial))
Exception goes: An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
Here is the code:
public HttpResponseMessage PostSales(List<Sales> Sales, [FromUri] string auth)
{
try
{
if (ModelState.IsValid)
{
if (auth == "KDI")
{
#region Stable but not multiline
//Int64 rs = db.Sales1.Where(sl => sl.Serial == Sales.Serial).Count();
//if (1 == rs)
//{
// return Request.CreateErrorResponse(HttpStatusCode.Conflict, " Duplicate Found!");
//}
//else
//{
// db.Sales1.Add(Sales);
// db.SaveChanges();
// return Request.CreateErrorResponse(HttpStatusCode.OK, "Added!");
//}
#endregion
Parallel.ForEach(Sales, s =>
{
if (db.Sales1.Any(sl => sl.ExtSerial != s.ExtSerial))
{
db.Sales1.Add(s);
db.SaveChanges();
}
else
{
return;
}
});
return Request.CreateErrorResponse(HttpStatusCode.OK, "Success!");
}
else
{
return Request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorized Access!");
}
}
else
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's wrong with the JSON model you sent me.");
}
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

I am using Janus(Third Party) Grid and getting the "System.StackOverflowException". Don't know how to solve it. I would like to appreciate for any help.
private void gridEX1_FormattingRow(object sender, RowLoadEventArgs e)
{
int index = e.Row.RowIndex;
try
{
if (!Convert.IsDBNull(gridEX1.GetRow(index).Cells["HEADER_ORDER_PACKAGE_ROW_ID"].Value))
{
if (Convert.ToInt32(gridEX1.GetRow(index).Cells["HEADER_ORDER_PACKAGE_ROW_ID"].Value) == PARENT_ORDER_PACKAGE_ID)
{
**gridEX1.MoveToRowIndex(index);**
GridEXRow curRow = gridEX1.GetRow();
if (curRow != null)
{
curRow.Expanded = true;
}
}
}
}
catch (Exception ex)
{
}
}
It seems that one of the lines inside your handler invoke the handler itself again. And so on, so you get StackOverflow.

Invalid Cross thread access in windows phone 7?

void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
XmlReader xmlDoc = XmlReader.Create(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(responseString)));
while (xmlDoc.Read())
{
if (xmlDoc.NodeType == XmlNodeType.Element)
{
if (xmlDoc.Name.Equals("ResponseCode"))
{
responseCode = xmlDoc.ReadInnerXml();
}
}
}
if (Convert.ToInt32(responseCode) == 200)
{
MessageBox.Show("Success");
}
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
}
catch (WebException e)
{
// Error treatment
// ...
}
}
in above code Messagebox.show dispalying "Invalid cross thread access".please tell me how to resolve this...
Dispatcher.BeginInvoke(() =>
MessageBox.Show("Your message")
);
Any interaction with the UI from code needs to be on the dispatcher thread, your callback from the HTTP request will not be running on this thread hence the error.
You should be able to use something like
Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.SHow("Success") );
to display the message box
HTH - Rupert.

Resources