This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
/* обычный console.log может превратиться в нечто большее */ | |
/* | |
как правило, начинающие программисты логируют по одной переменной, | |
мы же можем форматировать строки с любым количеством аргументов | |
*/ | |
console.log('Check:\r\n username - %s\r\n age - %i\r\n data - %o', 'Mike', 23, {status: 'registered'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
const wrapEventWithExcpetionHandler = (middleware) => (e) => { | |
const { error } = e; // предположим, что ошибка в этом поле | |
if (error) { | |
throw new Error(error); | |
} | |
try { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
const middlewareRequest = async (req) => { | |
try { | |
const { data } = await axios.get(req); | |
return data; | |
} catch (err) { | |
throw new Error(err); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* учитывайте, что глобальный метод window.onerror | |
не будет ловить исключения без Vue.config.errorHandler */ | |
Vue.config.errorHandler = (error, vm, info) => { | |
return middlewareErrorHandler(error, info); | |
} | |
// сюда попадут все ошибки, которые находятся за пределами исполнения Vue | |
window.onerror = ( | |
message, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule, Errorhandler } from '@angular/core'; | |
@Injectable | |
export class CustomErrorHandler extends ErrorHandler { | |
handleError(error) { | |
return middlewareErrorHandler(error); | |
} | |
} | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
/* обязательно ознакомьтесь с документацией https://ru.reactjs.org/docs/error-boundaries.html */ | |
/* к сожалению сейчас hooks не поддерживают работу с методом React Lifecycle API: componentDidCatch */ | |
export const withErrorBoundaries = (Wrapped) => { | |
class ErrorBoundary extends React.PureComponent { | |
/* укажите уникальное название компонента, для упрощения отладки в DevTools */ | |
static displayName = `ComponentWithErrorBoundary(${Wrapped.name || 'Wrapped'})`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* middleware может принимать любое количество аргументов любых типов */ | |
const middleware = (...args) => { | |
const [event, ...data] = args; | |
if (event.error) { | |
/* поймали объект экземпляр Error */ | |
return event.error; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const uncaughtExceptionHandler = (app) => { | |
/* Считаем, что все middleware функции подключаются в качестве модулей */ | |
process.on('error', middlewareErrorHandler); | |
process.on('uncaughtException', middlewareExceptionHandler); | |
/* В данном случае подразумеваем, что наше приложение может обслуживать | |
несколько микросервисов по типу app, поэтому ловим все ошибки в сервисе process */ | |
app | |
.on('error', middlewareErrorHandler); | |
}; |