Mailspring/internal_packages/composer/lib/send-action-button.cjsx
Evan Morikawa d4db0737cf feat(error): improve error reporting. Now NylasEnv.reportError
Summary:
The goal is to let us see what plugins are throwing errors on Sentry.

We are using a Sentry `tag` to identify and group plugins and their
errors.

Along the way, I cleaned up the error catching and reporting system. There
was a lot of duplicate error logic (that wasn't always right) and some
legacy Atom error handling.

Now, if you catch an error that we should report (like when handling
extensions), call `NylasEnv.reportError`. This used to be called
`emitError` but I changed it to `reportError` to be consistent with the
ErrorReporter and be a bit more indicative of what it does.

In the production version, the `ErrorLogger` will forward the request to
the `nylas-private-error-reporter` which will report to Sentry.

The `reportError` function also now inspects the stack to determine which
plugin(s) it came from. These are passed along to Sentry.

I also cleaned up the `console.log` and `console.error` code. We were
logging errors multiple times making the console confusing to read. Worse
is that we were logging the `error` object, which would print not the
stack of the actual error, but rather the stack of where the console.error
was logged from. Printing `error.stack` instead shows much more accurate
stack traces.

See changes in the Edgehill repo here: 8c4a86eb7e

Test Plan: Manual

Reviewers: juan, bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D2509
2016-02-03 18:06:52 -05:00

146 lines
4.4 KiB
CoffeeScript

_ = require 'underscore'
_str = require 'underscore.string'
{React, Actions, ExtensionRegistry} = require 'nylas-exports'
{Menu, RetinaImg, ButtonDropdown} = require 'nylas-component-kit'
class SendActionButton extends React.Component
@displayName: "SendActionButton"
@propTypes:
draft: React.PropTypes.object
isValidDraft: React.PropTypes.func
@CONFIG_KEY: "core.sending.defaultSendType"
constructor: (@props) ->
@state =
actionConfigs: @_actionConfigs(@props)
selectedSendType: NylasEnv.config.get(SendActionButton.CONFIG_KEY) ? @_defaultActionConfig().configKey
componentDidMount: ->
@unsub = ExtensionRegistry.Composer.listen(@_onExtensionsChanged)
componentWillReceiveProps: (newProps) ->
@setState actionConfigs: @_actionConfigs(newProps)
componentWillUnmount: ->
@unsub()
primaryClick: => @_onPrimaryClick()
_configKeyFromTitle: (title) ->
return _str.dasherize(title.toLowerCase())
_onExtensionsChanged: =>
@setState actionConfigs: @_actionConfigs(@props)
_defaultActionConfig: ->
title: "Send"
iconUrl: null
onSend: ({draft}) -> Actions.sendDraft(draft.clientId)
configKey: "send"
_actionConfigs: (props) ->
return [] unless props.draft
actionConfigs = [@_defaultActionConfig()]
for extension in ExtensionRegistry.Composer.extensions()
try
actionConfig = extension.sendActionConfig?({draft: props.draft})
if actionConfig
@_verifyConfig(actionConfig, extension)
actionConfig.configKey = @_configKeyFromTitle(actionConfig.title)
actionConfigs.push(actionConfig)
catch err
NylasEnv.reportError(err)
return actionConfigs
_verifyConfig: (config={}, extension) ->
name = extension.name
if not _.isString(config.title)
throw new Error("#{name}.sendActionConfig must return a string `title`")
if not _.isFunction(config.onSend)
throw new Error("#{name}.sendActionConfig must return a `onSend` function that will be called when the action is selected")
return true
render: ->
return false if not @props.draft
if @state.actionConfigs.length is 1
@_renderSingleDefaultButton()
else
@_renderSendDropdown()
_onPrimaryClick: =>
actionConfigs = @_orderedActionConfigs()
@_sendWithAction(actionConfigs[0].onSend)
_renderSingleDefaultButton: ->
classes = "btn btn-toolbar btn-normal btn-emphasis btn-text btn-send"
iconUrl = @state.actionConfigs[0].iconUrl
<button className={classes}
style={order: -100}
onClick={@_onPrimaryClick}>{@_sendContent(iconUrl)}</button>
_renderSendDropdown: ->
actionConfigs = @_orderedActionConfigs()
<ButtonDropdown
className={"btn-send btn-emphasis btn-text"}
style={order: -100}
primaryItem={@_sendContent(actionConfigs[0].iconUrl)}
primaryTitle={actionConfigs[0].title}
primaryClick={@_onPrimaryClick}
closeOnMenuClick={true}
menu={@_dropdownMenu(actionConfigs[1..-1])}/>
_orderedActionConfigs: ->
configKeys = _.pluck(@state.actionConfigs, "configKey")
if @state.selectedSendType not in configKeys
selectedSendType = @_defaultActionConfig().configKey
else
selectedSendType = @state.selectedSendType
primary = _.findWhere(@state.actionConfigs, configKey: selectedSendType)
rest = _.reject @state.actionConfigs, (config) ->
config.configKey is selectedSendType
return [primary].concat(rest)
_sendWithAction: (onSend) ->
isValidDraft = @props.isValidDraft()
if isValidDraft
try
onSend({draft: @props.draft})
catch err
NylasEnv.reportError(err)
_dropdownMenu: (actionConfigs) ->
<Menu items={actionConfigs}
itemKey={ (actionConfig) -> actionConfig.configKey }
itemContent={ (actionConfig) => @_sendContent(actionConfig.iconUrl) }
onSelect={@_menuItemSelect}
/>
_menuItemSelect: (actionConfig) =>
@setState selectedSendType: actionConfig.configKey
_sendContent: (iconUrl) ->
sendIcon = "icon-composer-send.png"
if iconUrl
plusHTML = <span>&nbsp;+&nbsp;</span>
additionalImg = <RetinaImg url={iconUrl}
mode={RetinaImg.Mode.ContentIsMask} />
else
plusHTML = ""
additionalImg = ""
<span>
<RetinaImg name={sendIcon} mode={RetinaImg.Mode.ContentIsMask} />
<span className="text">Send{plusHTML}</span>{additionalImg}
</span>
module.exports = SendActionButton