reactjs - React.js 在 Sentry 中记录所有 GraphQL 查询
问题描述
我有一个React应用程序,它周围有一个ErrorBoundary
向 Sentry 发送错误的应用程序,它工作正常。我也想将我所有的 GraphQL 查询错误记录到 Sentry 中,但我现在的问题是我的所有 GraphQL 查询,我有一个 catch 块,我在其中为失败的查询调度一个操作。当我删除 catch 块时,错误会记录到 Sentry 中,但我无法触发失败的查询操作。
我现在的解决方案是放入Sentry.captureException()
一个非常重复的 GraphQL 查询的每个 catch 块。
ErrorBoundary
即使查询有自己的 catch 块,有没有办法允许仍然捕获 GraphQL 错误?
function getEmployee() {
return function(dispatch) {
dispatch(requestEmployeeInformation());
GraphqlClient.query({ query: EmployeeQuery, fetchPolicy: 'network-only' })
.then((response) => {
dispatch(receiveEmployeeInformation(response.data));
})
.catch((error) => {
/* temporary solution. This sends error to sentry but is very repetitive because
it has to be added to every single action with a graphql query
*/
Sentry.captureException(error)
//dispatch this action if the query failed
dispatch(failGetEmployee(error));
});
};
}
解决方案
您总是可以在 catch 块内再次抛出错误。但是,处理此问题的最佳方法是使用Error Link。这将允许您记录 GraphQL 错误(作为响应的一部分返回的错误)以及网络错误(失败的请求、无效的查询等)。
import { onError } from '@apollo/link-error'
const link = onError(({ graphQLErrors, networkError, response }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
Sentry.captureMessage(message)
)
if (networkError) {
Sentry.captureException(networkError)
}
// Optionally, set response.errors to null to ignore the captured errors
// at the component level. Omit this if you still want component-specific handling
response.errors = null
});
推荐阅读
- node.js - 内容安全策略问题节点 js 反应
- nginx - CERTBOT 在 NGINX 服务器中找不到“SUBDOMAIN”的有效 IP 地址
- r - 如何根据 var1 (x) 的不同截止值绘制 %Sensitivity (y) 并为 R 上的每个观察值显示一个点?
- javascript - javascript 根据 url 参数从数组中获取属性
- pyspark - PySpark - 使用 UTF-16 编码解析 CSV 文件
- magento - 在本地安装 Magento:“同意并设置”按钮丢失
- android - 除非清除 ram,否则无法将数据持久保存在 Room 数据库中
- python - Python RGB 到灰度转换(ExGG)矩阵运算
- scala - 窗口上的 Spark 3 类型化用户定义聚合函数
- c++ - 如何声明和定义将返回编译时未知的少数自定义类之一的函数?