diff --git a/CHANGELOG.md b/CHANGELOG.md index b54fa7ae..16c45200 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- Client-side logging. + ## [0.6.15] - 2020-02-14 ### Added diff --git a/react/components/SearchQuery.js b/react/components/SearchQuery.js index ff04ba68..45c652a5 100644 --- a/react/components/SearchQuery.js +++ b/react/components/SearchQuery.js @@ -2,12 +2,14 @@ import { useState } from "react"; import { useQuery } from "react-apollo"; import PropTypes from "prop-types"; import { path, pathOr, isEmpty, reject } from "ramda"; +import { useRuntime } from "vtex.render-runtime"; import { onSearchResult } from "vtex.sae-analytics"; import BiggyClient from "../utils/biggy-client.ts"; import { makeFetchMore, fromAttributesToFacets, } from "../utils/compatibility-layer.ts"; +import logError from "../utils/log"; import searchResultQuery from "../graphql/searchResult.gql"; @@ -23,6 +25,7 @@ const SearchQuery = ({ variables, order, }) => { + const { account, workspace } = useRuntime(); const [page, setPage] = useState(1); const searchResult = useQuery(searchResultQuery, { @@ -35,6 +38,10 @@ const SearchQuery = ({ }, }); + if (searchResult.error) { + logError(account, workspace, attributePath, searchResult.error); + } + const redirect = path(["data", "searchResult", "redirect"], searchResult); searchResult.loading = searchResult.loading || redirect; diff --git a/react/utils/log.ts b/react/utils/log.ts new file mode 100644 index 00000000..12a5b633 --- /dev/null +++ b/react/utils/log.ts @@ -0,0 +1,31 @@ +import axios from "axios"; + +const client = axios.create({ + baseURL: "https://search.biggylabs.com.br/search-api/v1", + headers: { + "Content-Type": "application/json", + }, +}); + +const logError = ( + store: string, + workspace: string, + attributePath: string, + error: any, +) => { + const browser = + typeof navigator !== "undefined" + ? navigator.userAgent + : "server-side-error"; + + const message = `Workspace: ${workspace}\nBrowser: ${browser}\nMessage: ${ + error.message + }\n${error.stack != null ? error.stack : ""}`; + + client.post(`/${store}/log`, { + message, + url: `Search App Error at: ${attributePath}`, + }); +}; + +export default logError;