Immediately blow up if the user passes incorrect query params

Summary: If state.selectedId is null, or otherwise cannot find item, don't throw exception

Test Plan: Run tests

Reviewers: evan

Reviewed By: evan

Differential Revision: https://review.inboxapp.com/D1373
This commit is contained in:
Ben Gotow 2015-03-30 17:25:08 -07:00
parent f19f178e3c
commit 6ebe1118eb
3 changed files with 17 additions and 4 deletions

View file

@ -88,6 +88,8 @@ DraftList = React.createClass
_onShiftSelectedIndex: (delta) -> _onShiftSelectedIndex: (delta) ->
item = _.find @state.items, (draft) => draft.id is @state.selectedId item = _.find @state.items, (draft) => draft.id is @state.selectedId
return unless item
index = if item then @state.items.indexOf(item) else -1 index = if item then @state.items.indexOf(item) else -1
index = Math.max(0, Math.min(index + delta, @state.items.length-1)) index = Math.max(0, Math.min(index + delta, @state.items.length-1))
@setState @setState
@ -95,6 +97,7 @@ DraftList = React.createClass
_onDeleteSelected: -> _onDeleteSelected: ->
item = _.find @state.items, (draft) => draft.id is @state.selectedId item = _.find @state.items, (draft) => draft.id is @state.selectedId
return unless item
DatabaseStore.localIdForModel(item).then (localId) -> DatabaseStore.localIdForModel(item).then (localId) ->
Actions.destroyDraft(localId) Actions.destroyDraft(localId)

View file

@ -28,7 +28,7 @@ Mixin =
@setState getViewsByName(@components) @setState getViewsByName(@components)
componentWillUnmount: -> componentWillUnmount: ->
@_componentUnlistener() @_componentUnlistener() if @_componentUnlistener
# Internal representation of components # Internal representation of components
class Component class Component

View file

@ -288,16 +288,20 @@ DatabaseStore = Reflux.createStore
# ActiveRecord-style Querying # ActiveRecord-style Querying
find: (klass, id) -> find: (klass, id) ->
throw (new Error "find takes a string Id. You may have intended to use findBy.") unless _.isString(id) throw new Error("You must provide a class to findByLocalId") unless klass
throw new Error("find takes a string id. You may have intended to use findBy.") unless _.isString(id)
new ModelQuery(klass, @).where({id:id}).one() new ModelQuery(klass, @).where({id:id}).one()
findBy: (klass, predicates = []) -> findBy: (klass, predicates = []) ->
throw new Error("You must provide a class to findBy") unless klass
new ModelQuery(klass, @).where(predicates).one() new ModelQuery(klass, @).where(predicates).one()
findAll: (klass, predicates = []) -> findAll: (klass, predicates = []) ->
throw new Error("You must provide a class to findAll") unless klass
new ModelQuery(klass, @).where(predicates) new ModelQuery(klass, @).where(predicates)
count: (klass, predicates = []) -> count: (klass, predicates = []) ->
throw new Error("You must provide a class to count") unless klass
new ModelQuery(klass, @).where(predicates).count() new ModelQuery(klass, @).where(predicates).count()
# Support for Local IDs # Support for Local IDs
@ -306,12 +310,17 @@ DatabaseStore = Reflux.createStore
# (like body, stored in a separate table) are always included. # (like body, stored in a separate table) are always included.
# #
findByLocalId: (klass, localId) -> findByLocalId: (klass, localId) ->
return Promise.reject(new Error("You must provide a class to findByLocalId")) unless klass
return Promise.reject(new Error("You must provide a local Id to findByLocalId")) unless localId
new Promise (resolve, reject) => new Promise (resolve, reject) =>
@find(LocalLink, localId).then (link) => @find(LocalLink, localId).then (link) =>
return reject("Find by local ID lookup failed") unless link return reject("Find by local ID lookup failed") unless link
query = @find(klass, link.objectId).includeAll().then(resolve) query = @find(klass, link.objectId).includeAll().then(resolve)
bindToLocalId: (model, localId) -> bindToLocalId: (model, localId) ->
return Promise.reject(new Error("You must provide a model to bindToLocalId")) unless model
new Promise (resolve, reject) => new Promise (resolve, reject) =>
unless localId unless localId
if isTempId(model.id) if isTempId(model.id)
@ -325,10 +334,11 @@ DatabaseStore = Reflux.createStore
.catch(reject) .catch(reject)
localIdForModel: (model) -> localIdForModel: (model) ->
return Promise.reject(new Error("You must provide a model to localIdForModel")) unless model
new Promise (resolve, reject) => new Promise (resolve, reject) =>
if @_localIdLookupCache[model.id] if @_localIdLookupCache[model.id]
resolve(@_localIdLookupCache[model.id]) return resolve(@_localIdLookupCache[model.id])
return
@findBy(LocalLink, {objectId: model.id}).then (link) => @findBy(LocalLink, {objectId: model.id}).then (link) =>
if link if link