Different kinds of exception handling in nestjs (graphql) - rest

I am using GraphQLFederationModule in my nestjs project and now working on the exceptions handling part. I use formatError to catch all the exceptions thrown in resolvers.
Basically I have 4 kinds of source of exception:
Error in resolver/service logic that not related to other services.
Error from database, which is mongoDb (using nestjs/mongoose).
Error when calling api from other graphql servers using graphql-request.
Error when calling api from other restful api servers using axios.
For each kind of exceptions I need to handle it differently. So the question is, how can I determine which kind of exception it is from the error passed into formatError ?
Thank you.

I cannot find a "good" way to solve this so I just determine them by the content:
https://github.com/VictorCheng114668/nestjs-helper/blob/main/graphqlModule-formatError-helper/exceptionsHandler.ts

Related

Get detailed error message from AWS gateway validator in case of using multiple schemas for one endpoint

I'm using an openapi specification, which configures AWS gateway. I'm also using request validation. It works fine with informative error messages if I print out $context.error.validationErrorString in case of 4xx error messages. (In case of missing request data, It will write out exactly which request data is missing)
In case of using multiple schemas for an endpoint in the openapi spec (with using oneOf), validation also works, but the error message does not contain specific message, just that schema matching was not succesfull [instance failed to match exactly one schema (matched 0 out of 1)
I think the problem is that schema validation happens first, so api gw throws this error first. Is there any possibility to find out what is the exact request validation error in this way, so maybe fore api gw to run also the request validation?
Confirmed with AWS support that this is not possible at the moment.

Apache geode failing with OOM exception

I am trying to create a REST application over Apache geode. Application works well in case of limited data, but in cases when I need to get the complete data ( ~0.8M ), it fails with an OOM exception on server.
Exception :
HTTP GET Error: 500
REST OQL Response: {"cause":"Handler dispatch failed; nested exception is java.lang.OutOfMemoryError: Java heap space"}
I tried the same approach with cache client, it works seamlessly, but we need to use REST to integrate with our application.
Any ideas to go around with this?
I am thinking on the following approaches.
Can we break the data on server side and use something like "Range" with Apache Geode? I tried this quickly, but did not work well.
Can we start getting the data into smaller buffers at the client side and start reading buffer by buffer?
Is it possible to get data out from Geode as a data-stream?
Thanks,
Abhay
Would it be possible for your to share the stack trace for the OOM you are getting? Are you saying the results are 0.8 megabytes? That doesn't seem like it should cause on OOME.
You can get ranges of data using OQL queries, but if your data set is really that small it seems like something else strange is going on with the REST API.

Error Using DbExecutionStrategy About Streaming but Streaming not used

I'm trying to use a custom DbExecutionStrategy that implements retries, but I'm getting the following error:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Streaming queries are not supported by the configured execution strategy 'MyDbExecutionStrategy'. See http://go.microsoft.com/fwlink/?LinkId=309381 for additional information.
If you follow that link it will explain that EF6 doesn't use streaming by default but that you can turn it on by using AsStreaming().
By default, EF6 and later version will buffer query results rather than streaming them. If you want to have results streamed you can use the AsStreaming method to change a LINQ to Entities query to streaming.
However, the call that is being made does not use streaming and I don't have any code that calls AsStreaming().
dataModel.Set<DeploymentLog>().OrderByDescending(d => d.DeploymentTimestamp).FirstOrDefault()
I've copied the code and the strategy to a different command line app that I created to test it and it works fine there. But in my web application I'm getting that error.
Any ideas why? Is there some kind of setting to turn on streaming on all queries?

Breezejs - get detailed error message in queries

I am working on an application with ASP.Net/MVC/EF/Breeze/Angular and it is working in my development environment. I just deployed it to my test environment and I am getting an error when I try to query breeze entity data. I don't think it's a DB connection problem, because I can query the breeze metadata successfully.
However, when I try to query for entity data, for example:
http://server/path/breeze/data/Cities
I get the following error:
{"$id":"1","$type":"System.Web.Http.HttpError, System.Web.Http","Message":"An error has occurred."}
This shows up as a 500 error in the browser network history. Is there any way to get Breeze to return a more detailed error message? I am not getting this error in development so I can't debug it there. Or any suggestions as to what I should look for if I can get metadata but not data?
Well, it turns out it was a connection string problem! I have no idea how breeze was generating metadata without a DB connection. Maybe it caches it in a local file?
Anyways, it would be nice to be able to get more detailed messages in cases like this. Since breeze returns IQueryable<T> I couldn't see any obvious way to catch the exception and send back a message myself...

Logging logic and data errors in MVC3 with Elmah

I have a Service layer in my MVC3 app, which plays the role of a Repository among other things, as a layer between my Data layer and the actual web application. I have coded all my GetById methods to be robust, using FirstOrDefault and not just First, because the Id is passed in a URL and cannot be guaranteed to be a valid Id.
I now find myself where I'm doing a FirstOrDefault, then only proceeding if the result is not null. I would like to log the event when it is null, and then proceed to do nothing etc. Now, I am already using Elmah to log unhandled exceptions, and I have very little experience with exception handling etc. in MVC3, but it occurs to me that it might be better for me to use a simple First, with Elmah logging the exception if no entity is found.
How should I approach this scenario, where an invalid Id is quite definitely an logic exception, but not a low level CLR exception? This is not like when somebody is asked to enter an Id and no entity is found for their search term, which is a normal logic result.
Generating exceptions can be expensive. You're initial approach of validating user input is more robust. I would recommend using a logging framework such as NLog (http://nlog-project.org) to log the case were an invalid ID is passed in.
If you would like to keep all of your log messages in Elmah, then you can decide to write directly to Elmah's error log instead of bubbling-up an exception.