Skip to content

Commit 6149e59

Browse files
Nikitaskvav
authored andcommitted
Avoid scrape error message duplication on UI, adjust output
1 parent a14bb6e commit 6149e59

File tree

5 files changed

+53
-49
lines changed

5 files changed

+53
-49
lines changed

src/UI/ScrapeError.jsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react'
2+
import { BreakingPre } from './BreakingPre'
3+
import * as errors from '../errors'
4+
5+
function stripErrorCode (error) {
6+
return error.message.replace(/^\[[A-Z]{3}] /, '')
7+
}
8+
9+
function cleanStack (error) {
10+
const messagePrefix = stripErrorCode(error) + '\n'
11+
12+
const stack = error.stack.replace(/^Error: /, '')
13+
return stack.startsWith(messagePrefix) ? stack.slice(messagePrefix.length) : stack
14+
}
15+
16+
const Warn = ({children}) => {
17+
return <div>⚠️ {children}</div>
18+
}
19+
20+
export const ScrapeError = ({ error }) => {
21+
const logIsNotImportant = error.allowRetry || error.allow_retry
22+
const forcePluginReinstall = error.fatal
23+
const stack = cleanStack( error)
24+
25+
return (
26+
<div>
27+
<h3>Scrape Error</h3>
28+
{error.name === 'Error' && <Warn>This error message will never be displayed on production UI; if you intend to show meaningful message to user, use one of constructors:
29+
<ul>
30+
{Object.keys(errors).map((key) => <li key={key}>{key}</li>)}
31+
</ul>
32+
</Warn>}
33+
{logIsNotImportant && <Warn>logIsNotImportant = true: This error will be displayed on production UI without [Send log] button</Warn>}
34+
{forcePluginReinstall && <Warn>forcePluginReinstall = true: User will be forced into preferences screen after this error</Warn>}
35+
<BreakingPre>{error.message || 'error.message n/a'}</BreakingPre>
36+
<BreakingPre>{stack}</BreakingPre>
37+
</div>
38+
)
39+
}

src/UI/index.jsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import _ from "lodash";
2-
import React from "react";
1+
import _ from 'lodash'
2+
import React from 'react'
3+
import { toDate } from '../common/dateUtils'
34
import { toAtLeastTwoDigitsString } from '../common/stringUtils'
4-
import {prettyDeepDiff} from "../diff";
5-
import {Account} from "./Account";
6-
import {BreakingPre} from "./BreakingPre";
7-
import {Bubble} from "./Bubble";
8-
import {border, fontColor, zenmoneyGreenColor, zenmoneyRedColor} from "./designSystem";
9-
import {DayTransactions} from "./Transaction";
10-
import {toDate} from '../common/dateUtils'
5+
import { prettyDeepDiff } from '../diff'
6+
import { Account } from './Account'
7+
import { Bubble } from './Bubble'
8+
import { border, fontColor, zenmoneyGreenColor, zenmoneyRedColor } from './designSystem'
9+
import { ScrapeError } from './ScrapeError'
10+
import { DayTransactions } from './Transaction'
1111

12-
const SidePane = ({children}) => <div style={{borderLeft: border, overflowY: "auto"}}>{children}</div>;
12+
const SidePane = ({ children }) => <div style={{borderLeft: border, overflowY: "auto"}}>{children}</div>;
1313

1414
class AccountsPane extends React.PureComponent {
1515
render() {
@@ -143,9 +143,7 @@ export class UI extends React.PureComponent {
143143
</React.Fragment>
144144
)}
145145
{scrapeState === ":scrape-state/success" && !scrapeResult.pluginDataChange && <div>pluginData: no changes</div>}
146-
{scrapeState === ":scrape-state/error" && <div>Scrape error:
147-
<BreakingPre>{scrapeError.message || "n/a"}</BreakingPre>
148-
</div>}
146+
{scrapeState === ":scrape-state/error" && <ScrapeError error={scrapeError} />}
149147
{workflowState === ":workflow-state/complete" && <div><br />Cheers!</div>}
150148
</SidePane>
151149
{scrapeState === ":scrape-state/success" && (

src/common/adapters.js

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import { ensureSyncIDsAreUniqueButSanitized, sanitizeSyncId, trimSyncId } from '
2626
import { toZenMoneyTransaction } from './converters'
2727
import { getMidnight, isValidDate, toISODateString } from './dateUtils'
2828
import { sanitize } from './sanitize'
29-
import { isDebug } from './utils'
3029

3130
i18n.init({
3231
resources: _.fromPairs([
@@ -469,30 +468,6 @@ function getPresentationError (error, isFirstRun) {
469468
return meaningfulError
470469
}
471470

472-
function augmentErrorWithDevelopmentHints (error) {
473-
if (isDebug()) {
474-
if (!(error instanceof ZPAPIError)) {
475-
error.message += '\n(The message above will never be displayed on production UI; use specific ZPAPIError child class from errors.js if you want to show meaningful message to user)'
476-
}
477-
if (error.allowRetry || error.allow_retry) {
478-
error.message += '\n(logIsNotImportant = true: the message above will be displayed on production UI without [Send log] button)'
479-
}
480-
if (error.fatal) {
481-
error.message += '\n(forcePluginReinstall = true: user will be forced into preferences screen)'
482-
}
483-
if (!(error instanceof Error)) {
484-
const err = new Error()
485-
Object.assign(err, error)
486-
err.message = error.message
487-
err.stack = error.stack
488-
err.fatal = error.fatal
489-
err.allowRetry = error.allowRetry
490-
error = err
491-
}
492-
}
493-
return error
494-
}
495-
496471
export function adaptScrapeToGlobalApi (scrape) {
497472
console.assert(typeof scrape === 'function', 'argument must be function')
498473

@@ -519,10 +494,10 @@ export function adaptScrapeToGlobalApi (scrape) {
519494
value
520495
} = unsealSyncPromise(resultHandled)
521496
if (state === 'rejected') {
522-
ZenMoney.setResult(augmentErrorWithDevelopmentHints(getPresentationError(value, isFirstRun)))
497+
ZenMoney.setResult(getPresentationError(value, isFirstRun))
523498
} else if (state === 'pending') {
524499
resultHandled.catch((e) => {
525-
ZenMoney.setResult(augmentErrorWithDevelopmentHints(getPresentationError(e, isFirstRun)))
500+
ZenMoney.setResult(getPresentationError(e, isFirstRun))
526501
})
527502
}
528503
}

src/handleMessageFromWindow.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { extractErrorDetails } from './utils'
21
import { ZPAPI } from './ZPAPI'
32

43
const messageHandlers = {
@@ -17,9 +16,7 @@ const messageHandlers = {
1716
} catch (error) {
1817
reply({
1918
type: ':events/scrape-error',
20-
payload: {
21-
message: extractErrorDetails(error)
22-
}
19+
payload: error
2320
})
2421
}
2522
},

src/utils.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
export const extractErrorDetails = (error) => [
2-
error.message,
3-
error.stack && error.stack.replace('Error: ' + error.message + '\n', '')
4-
].filter(Boolean).join('\n')
5-
61
export const waitForOpenDevtools = () => new Promise(function fn (resolve) {
72
const threshold = 200
83
const eatenWidth = window.outerWidth - window.innerWidth

0 commit comments

Comments
 (0)