Skip to content

Commit

Permalink
fix: convert peptalk error message explicit into a string
Browse files Browse the repository at this point in the history
Before, we assume that the error thrown by pep has a .message, something which is not type safe.
  • Loading branch information
nytamin committed Jun 21, 2022
1 parent bdf99f9 commit b43513b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/mse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MSE, VizEngine, VPlaylist, VProfile, VRundown, VShow } from './v-connection'
import { LocationType, PepResponse, PepTalkClient, PepTalkJS, startPepTalk } from './peptalk'
import { getPepErrorMessage, LocationType, PepResponse, PepTalkClient, PepTalkJS, startPepTalk } from './peptalk'
import { CommandResult, IHTTPRequestError } from './msehttp'
import { EventEmitter } from 'events'
import { AtomEntry, FlatEntry, flattenEntry } from './xml'
Expand Down Expand Up @@ -175,7 +175,7 @@ export class MSERep extends EventEmitter implements MSE {
await this.pep.get(`/config/profiles/${profileName}`, 1)
} catch (err) {
throw new Error(
`The profile with name '${profileName}' for a new rundown does not exist. Error is: ${err.message}.`
`The profile with name '${profileName}' for a new rundown does not exist. Error is: ${getPepErrorMessage(err)}.`
)
}
if (playlistID) {
Expand All @@ -188,7 +188,7 @@ export class MSERep extends EventEmitter implements MSE {
}
playlistExists = true
} catch (err) {
if (err.message.startsWith('Referenced playlist exists but')) {
if (getPepErrorMessage(err).startsWith('Referenced playlist exists but')) {
throw err
}
playlistExists = false
Expand Down Expand Up @@ -254,7 +254,7 @@ export class MSERep extends EventEmitter implements MSE {
} catch (err: any) {
err.path = 'ping'
err.status = 418
err.response = err.message
err.response = getPepErrorMessage(err)
throw err as IHTTPRequestError
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/peptalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,3 +681,8 @@ class PepTalk extends EventEmitter implements PepTalkClient, PepTalkJS {
export function startPepTalk(hostname: string, port?: number): PepTalkClient & PepTalkJS {
return new PepTalk(hostname, port)
}

/** Converts an error thrown by peptalk into a string */
export function getPepErrorMessage(err: unknown): string {
return (typeof err === 'object' && ((err as any).message || err?.toString())) || `${err}`
}
8 changes: 4 additions & 4 deletions src/rundown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
InternalElementIdWithCreator,
} from './v-connection'
import { CommandResult, createHTTPContext, HttpMSEClient, HTTPRequestError } from './msehttp'
import { InexistentError, LocationType, PepResponse } from './peptalk'
import { getPepErrorMessage, InexistentError, LocationType, PepResponse } from './peptalk'
import { CREATOR_NAME, MSERep } from './mse'
import { flattenEntry, AtomEntry, FlatEntry } from './xml'
import * as uuid from 'uuid'
Expand Down Expand Up @@ -145,7 +145,7 @@ export class Rundown implements VRundown {
await this.getElement(elementId)
throw new Error(`An internal graphics element with name '${elementId.instanceName}' already exists.`)
} catch (err) {
if (err.message.startsWith('An internal graphics element')) throw err
if (getPepErrorMessage(err).startsWith('An internal graphics element')) throw err
}
}

Expand Down Expand Up @@ -214,15 +214,15 @@ ${entries}
await this.getElement(elementId)
throw new Error(`An external graphics element with name '${elementId.vcpid}' already exists.`)
} catch (err) {
if (err.message.startsWith('An external graphics element')) throw err
if (getPepErrorMessage(err).startsWith('An external graphics element')) throw err
}
}

private async checkChannelMapWasBuilt() {
try {
await this.initialChannelMapPromise
} catch (err) {
console.error(`Warning: createElement: Channel map not built: ${err.message}`)
console.error(`Warning: createElement: Channel map not built: ${getPepErrorMessage(err)}`)
}
}

Expand Down

0 comments on commit b43513b

Please sign in to comment.