Comments (9)
from graphql-core.
@miracle2k: As messa pointed out, it suffices to call logger.error(err, exc_info=err)
to log the stack traces, you don't need the traceback module.
from graphql-core.
@Cito Maybe so. But still, I think this is decidedly a downgrade in the dev experience compared to @2. Literally every developer is going to have an exception in a resolver at some point, would not see any useful output on the console, and would then have to figure out how to see what is going on, and how to fix it.
Maybe in the future, when adapted to @3, libraries such as graphql-server-core
, flask-graphql
would do the logging, but why should they commit to use of the logging
module to output exceptions, when they can equally claim to be generic libraries, and error reporting should be up to the user.
I mean I can see both sides here, but it was certainly very frustrating to deal with this when upgrading.
from graphql-core.
Can you post a small example with code, actual output and expected output?
from graphql-core.
if result.errors:
data['errors'] = []
for err in result.errors:
data['errors'].append(str(err))
logger.error(err)
# TODO: This should be going to logger
traceback.print_tb(err.__traceback__)
This is likely a weakness in my Python understanding - I was expecting the traceback to added to logger through logger.error(err). In the end, I got the trackback from the error object as above.
I would have thought that the text representation of the error would at least have the file and line number (not the full traceback), or am I wrong in that assumption?
There doesn't seem to be anything in the manual regarding error handling, nor the anatomy of the GraphQL error object, unless I'm mistaken?
from graphql-core.
@andrewspode Line numbers in Python code are part of the traceback which is not part of the string representation of an error. You can create your own logging Formatter
that includes the traceback if you want (google for some examples). Or you can also raise the error and call logger.exception(err)
instead of logger.error(err)
in the exception handler.
The reference on Read the Docs was broken. The anatomy of the GraphQL error object is here now: https://graphql-core-next.readthedocs.io/en/latest/modules/error.html
GraphQL-core is the low-level library that provides the errors - everything else regarding error handling should be implemented and documented on the levels higher up which are implementing the GraphQL server (packages like Graphene-Django), depending on the environment (development, production etc.)
from graphql-core.
@messa Nice! That looks like the simplest solution. Just looked it up in the Python docs:
There are three keyword arguments which are inspected:
exc_info
which, if it does not evaluate as false, causes exception information to be added to the logging message. If an exception tuple (in the format returned by sys.exc_info()) or an exception instance is provided, it is used; otherwise,sys.exc_info()
is called to get the exception information.- The second optional keyword argument is
stack_info
, which defaults to False. If true, stack information is added to the logging message, including the actual logging call. Note that this is not the same stack information as that displayed through specifyingexc_info
: The former is stack frames from the bottom of the stack up to the logging call in the current thread, whereas the latter is information about stack frames which have been unwound, following an exception, while searching for exception handlers. - The third optional keyword argument is
extra
which can be used to pass a dictionary which is used to populate the__dict__
of the LogRecord created for the logging event with user-defined attributes. These custom attributes can then be used as you like. For example, they could be incorporated into logged messages.
from graphql-core.
I had previously tried using sys.exc_info() but because I wasn't handling the original error, it gave me no results.
@messa - your solution is by far the simplest and neatest - thank you!
Glad to see Errors are back in the documentation - that clears some things up. Hopefully this thread will come up in Google for other people suffering with the same issue - it's very hard to debug resolvers without line numbers! Alternatively, a little paragraph on this in your documents wouldn't go amiss.
from graphql-core.
graphql-core@2 used to, as for as I can see, log stack traces of Python errors automatically, and @3 no longer does. I think that is a real loss; Often, an exception message just does not tell you where the error is, and having to go in and manually figure out how to use the traceback module to access the stack trace is unnecessarily complicated.
from graphql-core.
Related Issues (20)
- subscribe breaking change in graphql version 3.3.0a3 HOT 1
- [Question] How to map operation variables to their uses? HOT 2
- Using Relays in Graphql-core raises a TypeError HOT 4
- `out_name` is not respected for input objects used as default argument value
- Segfault during schema parsing HOT 3
- Segfault during schema parsing
- `graphql.utilities.print_schema` module contents are hidden behind `utilities` package
- `deepcopy` fails if the schema has a directive with an enum argument
- Both `typing.ByteString` and `typing.Text` are deprecated and slated for removal in Python 3.14 HOT 3
- Add the ability to specify `GraphQLResolveInfo.context` type HOT 1
- I use middleware for that right now HOT 1
- construction fails for depth around > 200 / resource exhaustion? HOT 9
- Cancel resolver tasks if execution of an operation is terminated HOT 7
- Support for official @oneof directive HOT 1
- How do you run tests with an IDE? HOT 4
- Pregenerate and Caching GraphqlSchema with Pickle HOT 4
- Validation/coercion of input variables violates the GraphQL spec HOT 1
- Inquiry about the release of a stable version after 3.2.3 HOT 1
- Variable values ordering not kept during execution HOT 2
- How to pass additional information to GraphQLResolveInfo
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-core.