Firefox:Problem with Status in OnStateChange in Proxy environment - macos

I am trying to use the status to see if there is an error in C++ XPCOM component in my observer class on Mac.
OnStateChange(
nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
PRUint32 aStateFlags,
nsresult aStatus)
{
}
In proxy environment the aStatus parameter is always true though the browser fails to load the page.
In non-proxy environments it gives the proper value (error) in status.
You can see it if you try accessing http://10.1.3.3/ (some random IP). With a proxy the status is zero (success) and without a proxy you get an error value.
Should some parameters be set to get the proper error value?

This is the expected behavior if you use an HTTP proxy. A non-zero aStatus means "didn't get any response because of some error". On the other hand, a zero aStatus value means "there was some response, check nsIHttpChannel.responseStatus to see whether the request was successful". That's what you get if the server responds with "404 Not Found" for example - aStatus will be zero (you got a response back) but nsIHttpChannel.responseStatus will be 404.
It's the same with an HTTP proxy because the proxy will always send a response back, probably "502 Bad Gateway" if it couldn't connect to the server. That's what the browser gets so aStatus will be zero and nsIHttpChannel.responseStatus will be 502. So in you code you should do something like this:
OnStateChange(
nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
PRUint32 aStateFlags,
nsresult aStatus)
{
if (FAILED(aStatus))
{
// Got no response
}
else
{
nsCOMPtr<nsIHttpChannel> channel = do_QueryInterface(aRequest);
PRUint32 status = 0;
if (channel)
channel->GetResponseStatus(&status);
if (status >= 400)
{
// Got an HTTP error
}
else
{
// Success!
}
}
}

Related

Shopware 6: Cypress test - reset database failed

I try to cleanup my database with command cy.cleanUpPreviousState:
// mytest.cy.js
...
beforeEach(() => {
cy.cleanUpPreviousState()
})
...
the request was response with error:
CypressError
cy.request() failed trying to load:
http://my-route.dev.localhost:8005/cleanup
The app runs in docker container, using shyim/shopware-docker
Questions
What is wrong with my request/route?
Which controller has to take this request?
To find out what is wrong, have a log at the network tab request log.
Answering your second question: There is a special server spun up for this action. It is not a normal Shopware route.
See in the cypress.js - it is supposed to use psh.phar to clean-up when this URL is called.
const requestedUrl = request.url;
if (requestedUrl !== "/cleanup") {
response.end();
return;
}
return childProcess.exec(
`${PROJECT_ROOT}/psh.phar e2e:cleanup`,
[...]
server.listen(8005);
So things to check are:
Is that port forwarded to your docker container?
Are you using the development template and is psh.phar existing?

Cognito throws ErrCodeNotAuthorizedException when user is already confirmed

Why does cognito throw ErrCodeNotAuthorizedException "NotAuthorizedException" when the status of the user is already confirmed when making a request to cognito to confirm the user.
The documentation specifies that ErrCodeNotAuthorizedException is thrown when a user is not authorized.
https://docs.aws.amazon.com/sdk-for-go/api/service/cognitoidentityprovider/#CognitoIdentityProvider.ConfirmSignUp
How should we handle this case? As it would be unclear if we made a request with invalid client secret as it would throw the same error.
Since the code is the same for the unauthorized case and user already confirmed case, the only possible way to differentiate the cases is to match the awsErr.Message() which provides the clear description of the error.
if awsErr, ok := err.(awserr.Error); ok {
switch awsErr.Code() {
case cognitoidentityprovider.ErrCodeNotAuthorizedException:
if awsErr.Message() == "User cannot be confirm. Current status is CONFIRMED" {
log.Println("Handle user already confirmed")
} else {
log.Println("Handle not authorized case")
}
...
default:
}
}

Unable to map the error getting from the application deployed using lambda function

I am having springboot application deployed using a lambda function. Please find the below sample.
Controller
#RequestMapping(method = RequestMethod.GET, value = "/bugnlow/findByRegionId/{regionId}", produces = "application/json")
public ResponseEntity<List<Bunglow>> findAllBunglowsByRegionId(#PathVariable int regionId, #RequestParam int page, #RequestParam int size) {
Page<Bunglow> bunglows = bunglowsService.findAllBunglowsByRegionId(regionId, page, size);
if (bunglows.getContent().size() == 0){
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(bunglows.getContent());
}
Service
if the "regionid" is invalid, I am throwing a runtime exception that contains message "region id is invalid".
throw new RuntimeException(Constant.INVALID_REGION_ID);
I am getting the below response when testing it locally by sending the invalid region id.
[1]{
"timestamp": 1519577956092,
"status": 500,
"error": "Internal Server Error",
"exception": "java.lang.RuntimeException",
"message": "Region Id is invalid",
"path": "/***/bugnlow/findByRegionId/343333"
}
I deployed above application AWS using lambda function. When I send the request from the AWS API gateway to the deployed application I am getting the below error with Amazon response headers.
[2] Request: /bugnlow/findByRegionId/342324?page=0&size=5 Status: 500 Latency: 166 ms Response Body
{ "message": "Internal server error" }
In the particular endpoint, integration responses have already configured for Error 500. But didn't use a template configuring the content-type as application/json.
I able to get the localized error by setting it in the controller class with
ResponseEntity<?>
But then the List Bunglow not display as the example response value in Swagger UI.
I need to get exact response[1] from the AWS console. How to do it.
Instead of error 500, how can I send the "Region id is invalid" with the Http status 400 (bad request).
It's a great help, if someone can help me on this.
Thanks
I able to resolve my problem by creating a class with
#ControllerAdvice
and handle the Exception using
#ExceptionHandler
Each point I need to validate and respond the error, I created an custom Exception "BunglowCustomExceptionResponse" and catch the exception in the Advice class "BunglowControllerAdvice". Then send the response with the exception message with bad request response as below.
#ControllerAdvice
public class BunglowControllerAdvice {
#ExceptionHandler
public ResponseEntity handleCustomBunglowException(Exception e){
logger.info("***Exception occurred :" + e.getLocalizedMessage());
return new ResponseEntity<BunglowCustomExceptionResponse>(new
BunglowCustomExceptionResponse(e.getLocalizedMessage()), HttpStatus.BAD_REQUEST);
}
}
Then, I able to get the expected response similar to below with bad request status code 400.
{"responseMessage": "error message"}
I don't know this is the best way but able to resolve my problem. Anyway, thanks a lot for your time who viewed this and tried to help me.

Handling errors using Parse.com Cloud Code and javascript API

What is the best way to handle errors using Parse.com Cloud Code. I'm able to use console.log and Firebug to see when Parse Cloud Code throws an error, but I need some help with how to notify the client that something went wrong. Some sample code from both sides would really be great -- Cloud Code and client side javascript code.
I preferred it this way -
On Cloud Code make one ErrorHandler.JS file -
exports.sendError = function(response, message, data) {
console.log("Message - " + message + " Data - " + JSON.stringify(data)); // To print LOG on Cloud Code
// Moreover you can use any of - "console.error/warn" - as mentioned - https://parse.com/docs/cloud_code_guide#logging
response.error({
status : false, // Indicates EXECUTION STATUS - I am using "successHandler" also & using STATUS as "true"
message : message, // Refers to Error Message
data : data || {} // Error Object or your customized Object
});
}
& client side you will have all data to print if you want or you can just show alert message to the Users.
More over it's preferred to check both SERVER side as well as CLIENT side LOG for DEVELOPING purpose because PARSE Cloud Code stores only last 100 messages in LOG.
& In order to implement proper LOGGING you must made some custom procedures with proper storage structure in terms of CLASS.
Parse has a section on Error Handling for Promises.
For instance when running a query in Cloud Code
query.find().then(function(result){ ... },
function(error){
response.error("Error occurred: " + error.message);
}
That will send error message down the client.
As an experiment I tried response.error with various strings/objects, below is what each returned (the commment shows the return value to the client).
Essentially, it always returns code 141, and you can only return a string. I was surprised that passing the proverbial err object from an exception returned {} my guess is this is for security reasons. What I do not understand is why you can't console.log(err) on the server as this has caused me a lot of confusion when trying to figure out what is going on. You basically always need to do err.message in your console.log statements to figure out what's really going on.
response.error("Some String of text") // --> {code: 141, message: "Some String of text"}
response.error( new Error("My Msg") ) // --> {code: 141, message: "{}"}
try {
var x = asdf.blah;
}catch(err) {
return response.error(err.message); // --> {code: 141, message: "asdf is not defined"}
}
response.error( err ); // --> {code: 141, message: "{}"}
response.error( Parse.Error(Parse.Error.VALIDATION_ERROR, "My Text") ); // --> {code: 141, message: "An error has occurred"}

log stack trace when HTTP request return error in Jmeter

I want to log all error message for failed HTTP request. I am going to run the thread group for 1B users and I don't want to use the View Result Tree because it logs everything and log file will bloat.
Currently I am using Beanshell Assertion as below.
if (Boolean.valueOf(vars.get("DEBUG"))) {
if (ResponseCode.equals("200") == false) {
log.info(SampleResult.getResponseMessage());
log.info("There was some problem");
}
}
But in this case it just prints the error message but I am interested to log the stack trace returned by the server.
I also used this method as mention in this thread
for (a: SampleResult.getAssertionResults()) {
if (a.isError() || a.isFailure()) {
log.error(Thread.currentThread().getName()+": "+SampleLabel+": Assertion failed for response: " + new String((byte[]) ResponseData));
}
}
But in this case I don't get an object out of SampleResult.getAssertionResults() method and it doesn't display anything in case of HTTP request failure.
Any idea how to get the stacK trace?
I figured it out. SampleResult has one more method called getResponseDataAsString(). This method returns the response message. In case of error it contains the error message.

Resources