Skip to content

Commit 53ceb74

Browse files
authored
Merge pull request #398 from nrkno/fix/pharos-response
Fix: Handle weird Pharos response
2 parents 91185ba + da90a01 commit 53ceb74

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

  • packages/timeline-state-resolver/src/integrations/pharos

packages/timeline-state-resolver/src/integrations/pharos/connection.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ export class Pharos extends EventEmitter {
553553
})
554554
}
555555
public async command(method: 'GET' | 'POST' | 'DELETE' | 'PUT', url0: string, data0?: { [key: string]: Primitives }) {
556+
const orgError = new Error() // for later
556557
return new Promise((resolve, reject) => {
557558
const url = `${this._options.ssl ? 'https' : 'http'}://${this._options.host}${url0}${this._queryString}`
558559

@@ -599,7 +600,24 @@ export class Pharos extends EventEmitter {
599600
}
600601
})
601602
.catch((error) => {
602-
this.emit('error', new Error(`Error ${method}: ${error}`))
603+
if (error instanceof got.RequestError) {
604+
// There is a weird case where Pharos replies with a body that doesn't match the content-length.
605+
// Which causes node.js http.request to throw an error:
606+
// RequestError: Parse Error: Expected HTTP/, RTSP/ or ICE/
607+
const statusCode = error.response?.statusCode
608+
if (typeof statusCode === 'number' && statusCode >= 200 && statusCode <= 299) {
609+
// The request actually succeeded
610+
resolve(undefined)
611+
return
612+
}
613+
}
614+
615+
error.stack += `\nOriginal stack: ${orgError.stack}`
616+
617+
const emitError = new Error(`Error ${method} ${url} (${JSON.stringify(data)}): ${error}`)
618+
619+
emitError.stack += `\nOriginal stack: ${orgError.stack}`
620+
this.emit('error', emitError)
603621
reject(error)
604622
})
605623
})

0 commit comments

Comments
 (0)