Taug qab qhov yuam kev hauv React daim ntawv thov siv Sentry

Taug qab qhov yuam kev hauv React daim ntawv thov siv Sentry

Hnub no kuv yuav qhia koj txog kev ua yuam kev hauv lub sijhawm tiag tiag hauv daim ntawv thov React. Ib daim ntawv thov pem hauv ntej tsis yog feem ntau siv rau kev ua yuam kev. Qee lub tuam txhab feem ntau tso tawm cov kab laum, rov qab mus rau nws tom qab cov ntaub ntawv, kev sim, thiab lwm yam. Txawm li cas los xij, yog tias koj tuaj yeem hloov koj cov khoom kom zoo dua, ces cia li ua nws!

1. Vim li cas koj thiaj xav tau Sentry?

Kuv xav tias koj nyiam taug qab cov kab mob thaum lub sijhawm tsim khoom

Koj puas xav tias qhov no tsis txaus?

Okay, cia saib cov ntsiab lus.

Cov laj thawj zoo tshaj plaws rau cov neeg tsim khoom siv Sentry:

  • Tso cai rau koj kom tsis txhob muaj kev pheej hmoo thaum xa cov lej nrog qhov yuam kev
  • Pab QA nrog kev xeem lej
  • Tau txais kev ceeb toom ceev txog teeb meem
  • Muaj peev xwm kho qhov yuam kev sai sai
  • Tau txais ib qho yooj yim zaub ntawm qhov yuam kev hauv lub vaj huam sib luag admin
  • Xaiv qhov yuam kev los ntawm cov neeg siv / browser ntu

Cov laj thawj tseem ceeb rau CEO/Lead project

  • Txuag nyiaj (Sentry tuaj yeem ntsia tau rau ntawm koj cov servers)
  • Tau txais cov lus teb rau cov neeg siv
  • Nkag siab txog qhov tsis raug ntawm koj qhov project hauv lub sijhawm
  • Nkag siab txog cov teeb meem uas tib neeg muaj nrog koj lub app
  • Pab koj nrhiav qhov chaw uas koj cov neeg tsim khoom ua yuam kev

Kuv xav tias cov neeg tsim khoom yuav txaus siab rau cov lus no ua ntej. Koj tuaj yeem siv cov npe ntawm cov laj thawj no los ntxias koj tus thawj coj los koom ua ke Sentry.

Ceev faj nrog cov khoom kawg ntawm cov npe lag luam.

Koj puas tau txaus siab?

Taug qab qhov yuam kev hauv React daim ntawv thov siv Sentry

Sentry yog dab tsi?

Sentry yog qhov qhib qhov chaw taug qab daim ntawv thov uas pab cov neeg tsim khoom taug qab thiab txhim kho kev sib tsoo hauv lub sijhawm. Tsis txhob hnov ​​​​qab tias daim ntawv thov tso cai rau koj kom ua tau zoo thiab txhim kho cov neeg siv kev paub. Sentry txhawb JavaScript, Node, Python, PHP, Ruby, Java thiab lwm yam lus programming.

Taug qab qhov yuam kev hauv React daim ntawv thov siv Sentry

2. Nkag mus thiab tsim ib qhov project

  • Qhib koj tus account Sentry. Tej zaum koj yuav tau nkag mus. (Thov nco ntsoov tias Sentry tuaj yeem ntsia tau rau ntawm koj cov servers)
  • Cov kauj ruam tom ntej yog los tsim ib qhov project
  • Xaiv koj hom lus los ntawm cov npe. (Peb yuav xaiv React. Nyem "Tsim Project")

Taug qab qhov yuam kev hauv React daim ntawv thov siv Sentry

Customize koj daim ntawv thov. Ib qho piv txwv yooj yim ntawm kev sib xyaw Sentry rau hauv lub thawv tuaj yeem pom hauv qab no:

import * as Sentry from '@sentry/browser';
// Sentry.init({
//  dsn: "<https://[email protected]/1432138>"
// });
// should have been called before using it here
// ideally before even rendering your react app 

class ExampleBoundary extends Component {
    constructor(props) {
        super(props);
        this.state = { error: null };
    }

    componentDidCatch(error, errorInfo) {
      this.setState({ error });
      Sentry.withScope(scope => {
        Object.keys(errorInfo).forEach(key => {
          scope.setExtra(key, errorInfo[key]);
        });
        Sentry.captureException(error);
      });
    }

    render() {
        if (this.state.error) {
            //render fallback UI
            return (
              <a onClick={() => Sentry.showReportDialog()}>Report feedback</a>
            );
        } else {
            //when there's not an error, render children untouched
            return this.props.children;
        }
    }
}

Sentry muaj tus pab Wizard los pab koj paub seb koj yuav tsum ua dab tsi ntxiv. Koj tuaj yeem ua raws li cov kauj ruam no. Kuv xav qhia koj yuav ua li cas los tsim koj thawj qhov yuam kev handler. Zoo heev, peb tau tsim ib qhov project! Cia peb mus rau kauj ruam tom ntej

3. React thiab Sentry kev koom ua ke

Koj yuav tsum nruab npm pob hauv koj qhov project.

npm i @sentry/browser

Initialize Sentry hauv koj lub thawv:

Sentry.init({
 // dsn: #dsnUrl,
});

Lub DSN nyob rau hauv Projects -> Chaw -> Client Keys. Koj tuaj yeem pom cov neeg siv khoom yuam sij hauv qhov tshawb nrhiav bar.

Taug qab qhov yuam kev hauv React daim ntawv thov siv Sentry

componentDidCatch(error, errorInfo) {
  Sentry.withScope(scope => {
    Object.keys(errorInfo).forEach(key => {
      scope.setExtra(key, errorInfo[key]);
    });
    Sentry.captureException(error);
 });
}

4. Taug qab thawj qhov yuam kev

Piv txwv li, kuv siv ib daim ntawv thov suab paj nruag yooj yim nrog Deezer API. Koj tuaj yeem pom nws no. Peb yuav tsum tsim qhov yuam kev. Ib txoj hauv kev yog nkag mus rau cov cuab yeej "undefined".

Peb yuav tsum tsim ib lub pob uas hu console.log с user.email. Tom qab qhov kev txiav txim no peb yuav tsum tau txais cov lus yuam kev: Uncaught TypeError (tsis tuaj yeem nyeem cov cuab yeej los ntawm undefined email) vim tus neeg siv khoom ploj lawm. Koj kuj siv tau Javascript kev zam.

<button type="button" onClick={() => console.log(user.email)}>   
  Test Error button 
</button>

Tag nrho lub thawv zoo li no:

import React, { Component } from "react";
import { connect } from "react-redux";
import { Input, List, Skeleton, Avatar } from "antd";
import * as Sentry from "@sentry/browser";
import getList from "../store/actions/getList";

const Search = Input.Search;

const mapState = state => ({
  list: state.root.list,
  loading: state.root.loading
});

const mapDispatch = {
  getList
};

class Container extends Component {
  constructor(props) {
    super(props);

    Sentry.init({
      dsn: "https://[email protected]/1417586",
    });
  }

  componentDidCatch(error, errorInfo) {
    Sentry.withScope(scope => {
      Object.keys(errorInfo).forEach(key => {
        scope.setExtra(key, errorInfo[key]);
      });
      Sentry.captureException(error);
    });
  }
  render() {
    const { list, loading, getList } = this.props;
    const user = undefined;
    return (
      <div className="App">
        <button
          type="button"
          onClick={() => console.log(user.email)}
        >
          test error1
        </button>
        <div onClick={() => Sentry.showReportDialog()}>Report feedback1</div>
        <h1>Music Finder</h1>
        <br />
        <Search onSearch={value => getList(value)} enterButton />
        {loading && <Skeleton avatar title={false} loading={true} active />}
        {!loading && (
          <List
            itemLayout="horizontal"
            dataSource={list}
            locale={{ emptyText: <div /> }}
            renderItem={item => (
              <List.Item>
                <List.Item.Meta
                  avatar={<Avatar src={item.artist.picture} />}
                  title={item.title}
                  description={item.artist.name}
                />
              </List.Item>
            )}
          />
        )}
      </div>
    );
  }
}

export default connect(
  mapState,
  mapDispatch
)(Container);

Tom qab kev koom ua ke lub pob no, koj yuav tsum sim nws hauv qhov browser.

Taug qab qhov yuam kev hauv React daim ntawv thov siv Sentry

Peb muaj peb thawj qhov yuam kev

Taug qab qhov yuam kev hauv React daim ntawv thov siv Sentry

Whoo-hoo!

Taug qab qhov yuam kev hauv React daim ntawv thov siv Sentry

Yog tias koj nyem rau ntawm qhov yuam kev header, koj yuav pom ib pawg kab.

Taug qab qhov yuam kev hauv React daim ntawv thov siv Sentry

Cov lus zoo li phem. Tau kawg peb tau pom cov lus yuam kev tsis nkag siab qhov chaws nyob qhov twg. Los ntawm lub neej ntawd peb tab tom tham txog daim ntawv qhia chaw hauv ReactJS vim tias lawv tsis tau teeb tsa.

Kuv kuj xav muab cov lus qhia rau kev teeb tsa daim ntawv qhia chaw, tab sis qhov ntawd yuav ua rau kab lus no ntev dua li kuv xav.

Koj tuaj yeem kawm lub ntsiab lus no no. Yog koj txaus siab rau tsab xov xwm no, Dmitry Nozhenko yuav tshaj tawm qhov thib ob txog kev sib koom ua ke ntawm daim ntawv qhia chaw. Yog li ntawd, ntaus ntau nyiam thiab subscribe Dmitry Nozhenkokom tsis txhob nco qhov thib ob.

5. Kev siv Xyuam Xais nrog kawg point API

OK. Peb tau npog qhov kev zam ntawm javascript hauv cov kab lus dhau los. Txawm li cas los xij, peb yuav ua li cas txog XHR qhov yuam kev?

Sentry kuj muaj kev cai yuam kev. Kuv siv nws los taug qab api yuam kev.

Sentry.captureException(err)

Koj tuaj yeem hloov kho lub npe yuam kev, qib, ntxiv cov ntaub ntawv, cov ntaub ntawv siv tshwj xeeb siv koj daim ntawv thov, email, thiab lwm yam.

superagent
  .get(`https://deezerdevs-deezer.p.rapidapi.com/search?q=${query}`)
  .set("X-RapidAPI-Key", #id_key)
  .end((err, response) => {
    if (err) {
      Sentry.configureScope(
        scope => scope
          .setUser({"email": "[email protected]"})
          .setLevel("Error")
      );
      return Sentry.captureException(err);
    }

    if (response) {
      return dispatch(setList(response.body.data));
    }
  });

Kuv xav siv cov haujlwm dav dav rau kev ntes API.

import * as Sentry from "@sentry/browser";

export const apiCatch = (error, getState) => {
  const store = getState();
  const storeStringify = JSON.stringify(store);
  const { root: { user: { email } } } = store;

  Sentry.configureScope(
    scope => scope
      .setLevel("Error")
      .setUser({ email })
      .setExtra("store", storeStringify)
  );
    // Sentry.showReportDialog(); - If you want get users feedback on error
  return Sentry.captureException(error);
};

Import no muaj nuj nqi nyob rau hauv koj api hu.

export default query => (dispatch, getState) => {
  superagent
    .get(`https://deezerdevs-deezer.p.rapidapi.com/search?q=${query}`)
    .set("X-RapidAPI-Key", #id_key)
    .end((error, response) => {
      if (error) {
        return apiCatch(error, getState)
      }

      if (response) {
        return dispatch(setList(response.body.data));
      }
    });
};

Cia peb xyuas cov txheej txheem:

  • setLevel tso cai rau koj mus ntxig ib theem yuam kev rau hauv lub sendry dashboard. Nws muaj cov khoom - 'fatal', 'error', 'warning', 'log', 'info, 'debug', 'critical').
  • setUser pab txuag txhua tus neeg siv cov ntaub ntawv (id, email chaw nyob, phiaj xwm them nyiaj, thiab lwm yam).
  • setExtra tso cai rau koj qhia cov ntaub ntawv koj xav tau, piv txwv li, khaws cia.

Yog tias koj xav kom tau txais cov neeg siv lus tawm tswv yim txog kab laum, koj yuav tsum siv lub showReportDialog muaj nuj nqi.

Sentry.showReportDialog();

Xaus:

Niaj hnub no peb tau piav qhia txog ib txoj hauv kev los muab Sentry rau hauv daim ntawv thov React.

β†’ Telegram tham los ntawm Xyuam Xais

Tau qhov twg los: www.hab.com

Ntxiv ib saib