Cannot log stack traces with apollo server - apollo-server

I'm building a graphql application with apollo server and I'm having trouble figuring out how to log stack traces serverside in production mode.
I found these docs but it's not clear how to correctly log stack traces in production.
I have the following formatError function:
formatError: ((err) => {
console.error(err.originalError)
return err
})
and when one of my resolvers throws an error with NODE_ENV=production I see the following output:
Error: Unexpected error value: "could not get config value"
at locatedError (/tripvector/node_modules/graphql/error/locatedError.js:24:9)
at /tripvector/node_modules/graphql/execution/execute.js:491:54
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async execute (/tripvector/node_modules/apollo-server-core/dist/requestPipeline.js:204:20)
at async processGraphQLRequest (/tripvector/node_modules/apollo-server-core/dist/requestPipeline.js:138:28)
at async processHTTPRequest (/tripvector/node_modules/apollo-server-core/dist/runHttpQuery.js:187:30)
However, when I run in debug mode, the original error is printed as I expect:
Error: could not get config value
at validate (file:///Users/paymahn/code/tripvector/tripvector-mono/backend/lib/settings/settings.js:11:15)
at getGoogleSecret (file:///Users/paymahn/code/tripvector/tripvector-mono/backend/lib/settings/settings.js:29:12)
at Object.loginWithGoogle (file:///Users/paymahn/code/tripvector/tripvector-mono/backend/api/users/graphql/mutations.js:141:30)
at field.resolve (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/apollo-server-core/dist/utils/schemaInstrumentation.js:52:26)
at executeField (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/execution/execute.js:469:20)
at /Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/execution/execute.js:365:22
at promiseReduce (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/jsutils/promiseReduce.js:23:9)
at executeFieldsSerially (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/execution/execute.js:361:43)
at executeOperation (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/execution/execute.js:335:14)
at execute (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/execution/execute.js:130:20)
I throw the error with the following code:
function validate(val) {
if (!val) {
throw new Error('could not get config value')
}
return val
}
How can I correctly configure apollo server to print stack traces for errors, even in production?

As usual, I found the answer 2 minutes after asking. I was doing some silly error catching and rethrowing in promises which is why this problem was happening only in production.

Related

Can Cypress give me an error message when there has been an error?

I have the below snippet from my test case:
cy.get('item_here').should('not.exist');
Can cypress give me a custom error message when the "item_here" does exist?
Thanks,
You can chain a log message on to the existing code, and it will only run if the element does not exist.
cy.get('item_here').should('not.exist')
.then(() => cy.log('no such element found')) // Note; this is an additional log
Changing both "success" and "fail" messages is hard because Cypress likes to show the red AssertionError block when anything fails or an error is thrown.
You can use the should() callback version, but please use an expect() inside otherwise you do not have retry,
cy.get('item_here').should($el => {
expect($el, 'Cannot be found').to.not.exist // expect causes retry for 4 seconds
Cypress.log({
name: 'Missing',
message: 'Cannot be found'
})
})
You can throw your own errors in a callback function for .should().
cy.get(".does-not-exist")
.should("not.exist")
.then(($el) => {
if ($el == null) {
throw new Error("Item does not exist in DOM");
}
});

What defines the log type (Default, Alert, Error, Critical, etc) on logs out of a Cloud Run container instance?

I have an express server that is hosted on Cloud Run / Docker container.
This is the screen where we can view logs that come out of the deployed instance.
What defines the "type" of the log message: as in Alert, Critical, Error, Warning, Debug, Info, Notice and Default
If I log with console.error will it show up as an Error ?
What is the documentation on this subject?
UPDATE: Trying to log an error with the type Error
const logError = (msg: string | Error) => console.error(`[test:error] ${msg}`);
const testError = () : void => {
try {
throw new Error("TEST ERROR");
}
catch(err) {
const someError = new Error("HELLO ERROR");
console.log(someError);
console.error(someError);
logError(err);
logError("ERROR STRING MSG");
}
};
These were the results:
Not a single log with the type Error. Is this not supposed to be triggered by our code? When should it happen?
I'd like to filter logged messages from my catch blocks in some situations and I was hoping to filter for the Error log type. I guess I'll have to add the [error] string flag and filter for that.
How do people usually handle this?
Just install and use the Stackdriver node.js library if your goal is to send logs to Google Stackdriver (Operations Logging).

Turn off annoying mocha logs on Axios error

I am testing my application and I noticed a thing that is annoying me. So my code is below. I am trying to register a user that is existing in database and asserting a failure.
async function registerUserAgain() {
try {
const { status } = await http.post(registrationUrl, user);
assert.notStrictEqual(status, httpStatus.CREATED);
} catch({ code, response }) {
assert.strictEqual(response.status, httpStatus.CONFLICT);
}
}
describe('Testing registration scenarios', function () {
it('Duplicate user registration failure', registerUserAgain);
}
This code does exactly what I want. However when I look at the output
Request failed with status code 409
✓ Duplicate user registration failure
How to remove this first line from the output? Logs would be prettier and cleaner if mocha and axios would not log those things.
you can try the following command $ mocha --reporter min test or take a look at the other reporter options
This looks like some bug thing in mocha. Updating it to 5.2.0 solved the issue

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