Debugging 如何将Graphene Django/Graphene Python异常打印到控制台进行调试?

Debugging 如何将Graphene Django/Graphene Python异常打印到控制台进行调试?,debugging,exception,graphene-python,graphene-django,Debugging,Exception,Graphene Python,Graphene Django,当GraphQL错误发生时,我无法轻松知道它发生在何处。我不得不花不必要的时间去追查它。如何在文本编辑器的控制台中打印回溯?我通过访问带有result.errors的GraphQL错误,遍历列表,并使用python的print_tb函数打印回溯来回答自己的问题 有没有人有不同或更好的方法 print_graphql_errors函数的示例用法: print_graphql_errors函数: 来自回溯导入打印的 从django.conf.settings导入调试 def print_graphq

当GraphQL错误发生时,我无法轻松知道它发生在何处。我不得不花不必要的时间去追查它。如何在文本编辑器的控制台中打印回溯?

我通过访问带有result.errors的GraphQL错误,遍历列表,并使用python的print_tb函数打印回溯来回答自己的问题

有没有人有不同或更好的方法

print_graphql_errors函数的示例用法: print_graphql_errors函数: 来自回溯导入打印的

从django.conf.settings导入调试
def print_graphql_错误(错误,raise_错误=False):
如果未调试:
引发异常(
'DevError:不应在生产中调用此函数'
)
断言错误,'DevError:“errors”参数不能为None'
在=[]之后打印
当前错误=无
3月3日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日############################
print('手动生成的回溯(使用print_graphql_errors函数):')
打印(有{len(errors)}个错误:'))
对于i,枚举中的错误(错误):
打印(f'{i+1}',错误)
3月3日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日############################
对于错误中的错误:
当前错误=错误
#仅供参考:此对象具有以下属性:(示例属性值)
#tb_框架
#tb_lasti 16
#tb_lineno 447
#下一步
#打印('error.locations:',error.locations)
#打印('error.positions:',error.positions)
尝试:
打印\u tb(错误.堆栈)
除属性错误为e外:
打印(e.uu回溯)
打印(f'警告:尝试打印回溯时出错:{e}。它改为具有以下属性:{dir(错误)}')
在.append之后打印_(错误)
如果len(打印后):
3月3日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日############################
print(f'没有“stack”属性的错误数:{len(print_after)}')
3月3日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日############################
如果出现错误:
对于打印错误,请执行以下操作:
提出错误
提高电流_错误

我通过访问带有result.errors的GraphQL错误,遍历列表,并使用python的print_tb函数打印回溯来回答自己的问题

有没有人有不同或更好的方法

print_graphql_errors函数的示例用法: print_graphql_errors函数: 来自回溯导入打印的

从django.conf.settings导入调试
def print_graphql_错误(错误,raise_错误=False):
如果未调试:
引发异常(
'DevError:不应在生产中调用此函数'
)
断言错误,'DevError:“errors”参数不能为None'
在=[]之后打印
当前错误=无
3月3日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日############################
print('手动生成的回溯(使用print_graphql_errors函数):')
打印(有{len(errors)}个错误:'))
对于i,枚举中的错误(错误):
打印(f'{i+1}',错误)
3月3日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日############################
对于错误中的错误:
当前错误=错误
#仅供参考:此对象具有以下属性:(示例属性值)
#tb_框架
#tb_lasti 16
#tb_lineno 447
#下一步
#打印('error.locations:',error.locations)
#打印('error.positions:',error.positions)
尝试:
打印\u tb(错误.堆栈)
除属性错误为e外:
打印(e.uu回溯)
打印(f'警告:尝试打印回溯时出错:{e}。它改为具有以下属性:{dir(错误)}')
在.append之后打印_(错误)
如果len(打印后):
3月3日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日############################
print(f'没有“stack”属性的错误数:{len(print_after)}')
3月3日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日############################
如果出现错误:
对于打印错误,请执行以下操作:
提出错误
提高电流_错误
from django.conf.settings import DEBUG

result = schema.execute(
    mutation_str, context_value=request, variable_values=variable_values
)

if result.errors is None:
    return self.handle_success(result)
if DEBUG:
    print_graphql_errors(result.errors)

return self.handle_failure(result)

from traceback import print_tb
from django.conf.settings import DEBUG

def print_graphql_errors(errors, raise_error=False):
    if not DEBUG:
        raise Exception(
            'DevError: This function should not be called in production'
        )

    assert errors, 'DevError: The "errors" parameter cannot be None'
    print_after = []
    current_error = None

    print('######################################################################')
    print('Manually Generated Traceback (with the print_graphql_errors function):')
    print(f'There are {len(errors)} errors:')

    for i, error in enumerate(errors):
        print(f'{i + 1}) ', error)

    print('######################################################################')

    for error in errors:
        current_error = error
        # FYI: This object has these attributes: (example attribute values)
            # tb_frame <frame at 0x000002DDB844D548, file 'site-packages\\graphql\\execution\\executor.py', line 455, code resolve_or_error>
            # tb_lasti 16
            # tb_lineno 447
            # tb_next <traceback object at 0x000002DDBAFBA388>
        # print('error.locations:', error.locations)
        # print('error.positions:', error.positions)
        try:
            print_tb(error.stack)
        except AttributeError as e:
            print(e.__traceback__)
            print(f'Warning: An error occured while trying to print the traceback: {e}. It has the following attributes instead: {dir(error)}')
            print_after.append(error)

    if len(print_after):
        print('###################################################################')
        print(f'Number of errors without the "stack" attribute: {len(print_after)}')
        print('###################################################################')

    if raise_error:
        for error in print_after:
            raise error

        raise current_error