Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Errors in deserialize function #713

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions client/src/serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ const PRIMITIVES = [

function modifyObject(doc) {
Object.keys(doc).forEach(key => {
doc[key] = deserialize(doc[key])
doc[key] = deserialize(doc[key], true)
})
return doc
}

export function deserialize(value) {
class ProtocolError extends Error {
constructor(msg, errorCode) {
super(msg)
this.errorCode = errorCode
}
toString() {
return `${this.message} (Code: ${this.errorCode})`
}
}

export function deserialize(value, nested) {
if (value == null) {
return value
} else if (PRIMITIVES.indexOf(typeof value) !== -1) {
Expand All @@ -19,6 +29,8 @@ export function deserialize(value) {
const date = new Date()
date.setTime(value.epoch_time * 1000)
return date
} else if (value.error !== undefined && nested !== true) {
throw new ProtocolError(value.error, value.error_code)
} else {
return modifyObject(value)
}
Expand Down
13 changes: 0 additions & 13 deletions client/src/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ const STATUS_ERROR = { type: 'error' }
// Occurs when the socket closes
const STATUS_DISCONNECTED = { type: 'disconnected' }

class ProtocolError extends Error {
constructor(msg, errorCode) {
super(msg)
this.errorCode = errorCode
}
toString() {
return `${this.message} (Code: ${this.errorCode})`
}
}


// Wraps native websockets with a Subject, which is both an Subscriber
// and an Observable (it is bi-directional after all!). This version
Expand Down Expand Up @@ -176,9 +166,6 @@ export class HorizonSocket extends WebSocketSubject {
return this.sendHandshake().ignoreElements()
.concat(this.makeRequest(rawRequest))
.concatMap(resp => {
if (resp.error !== undefined) {
throw new ProtocolError(resp.error, resp.error_code)
}
const data = resp.data || []

if (resp.state !== undefined) {
Expand Down