Skip to content
Merged
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
Binary file modified screenshots/error-2160x3840.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/error-3840x2160.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 11 additions & 20 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ function apiUrl(instanceUrl: string, path: string): string {

export class AuthError extends Error {}

async function performApiRequest(
async function apiFetch<T>(
instanceUrl: string,
accessToken: string,
path: string,
method: 'GET' | 'PUT' = 'GET'
): Promise<Response> {
): Promise<T> {
let response: Response
try {
return await fetch(apiUrl(instanceUrl, path), {
response = await fetch(apiUrl(instanceUrl, path), {
method,
headers: {
Authorization: `Bearer ${accessToken}`,
Expand All @@ -30,27 +31,17 @@ async function performApiRequest(
{ cause: err }
)
}
}

function classifyApiResponse(res: Response, path: string): void {
if (res.status === 401) throw new AuthError(`Unauthorized: ${path}`)
if (res.status === 404)
if (response.status === 401) throw new AuthError(`Unauthorized: ${path}`)
if (response.status === 404)
throw new Error(
'The selected content could not be found. Please verify that it still exists in Salesforce.'
)
if (res.status >= 500 || res.status === 429)
throw new Error(`Salesforce's API had a problem (${res.status}).`)
if (!res.ok) throw new Error(`API error ${res.status}: ${path}`)
}
if (response.status >= 500 || response.status === 429)
throw new Error(`Salesforce's API had a problem (${response.status}).`)
if (!response.ok) throw new Error(`API error ${response.status}: ${path}`)

async function apiFetch<T>(
instanceUrl: string,
accessToken: string,
path: string
): Promise<T> {
const res = await performApiRequest(instanceUrl, accessToken, path)
classifyApiResponse(res, path)
return res.json() as Promise<T>
return response.json() as Promise<T>
}

export async function triggerDashboardRefresh(
Expand All @@ -59,7 +50,7 @@ export async function triggerDashboardRefresh(
contentId: string
): Promise<void> {
const path = `/analytics/dashboards/${contentId}`
await performApiRequest(instanceUrl, accessToken, path, 'PUT').catch(() => {})
await apiFetch(instanceUrl, accessToken, path, 'PUT').catch(() => {})
}

export async function getDashboardResults(
Expand Down
72 changes: 24 additions & 48 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,51 +42,38 @@ type CredentialsResult =
| { ok: true; accessToken: string; instanceUrl: string }
| { ok: false }

type LoadAttempt =
| { ok: true; content: ContentResults }
| { ok: false; error: unknown }

type ContentResults =
| { contentType: 'dashboard'; results: DashboardResults }
| { contentType: 'report'; results: ReportResult }

async function fetchContentResults(
contentType: SalesforceContentType,
instanceUrl: string,
accessToken: string,
contentId: string
): Promise<ContentResults> {
): Promise<DashboardResults | ReportResult> {
if (contentType === 'dashboard') {
await triggerDashboardRefresh(instanceUrl, accessToken, contentId)
const results = await getDashboardResults(
instanceUrl,
accessToken,
contentId
)
return { contentType, results }
return getDashboardResults(instanceUrl, accessToken, contentId)
}

const results = await getReportResults(instanceUrl, accessToken, contentId)
return { contentType, results }
return getReportResults(instanceUrl, accessToken, contentId)
}

async function loadContent(
context: RenderContext,
accessToken: string,
instanceUrl: string
): Promise<LoadAttempt> {
try {
const content = await fetchContentResults(
context.contentType,
instanceUrl,
accessToken,
context.contentId
)
writeCachedContent(context.contentType, context.contentId, content.results)
return { ok: true, content }
} catch (err) {
return { ok: false, error: err }
}
): Promise<void> {
const results = await fetchContentResults(
context.contentType,
instanceUrl,
accessToken,
context.contentId
)
writeCachedContent(context.contentType, context.contentId, results)
showContent({
contentType: context.contentType,
contentId: context.contentId,
results,
showLabels: context.showLabels,
} as ContentToRender)
}

function showCachedContent(context: RenderContext): void {
Expand Down Expand Up @@ -132,25 +119,14 @@ export async function render(context: RenderContext): Promise<void> {
const credentials = requireCredentials(context)
if (!credentials.ok) return

const attempt = await loadContent(
context,
credentials.accessToken,
credentials.instanceUrl
)

if (attempt.ok) {
showContent({
...attempt.content,
try {
await loadContent(context, credentials.accessToken, credentials.instanceUrl)
} catch (err) {
reportError(err, {
source: 'salesforce-content',
contentId: context.contentId,
showLabels: context.showLabels,
contentType: context.contentType,
})
return
handleContentFailure(context, err)
}

reportError(attempt.error, {
source: 'salesforce-content',
contentId: context.contentId,
contentType: context.contentType,
})
handleContentFailure(context, attempt.error)
}
7 changes: 5 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {
signalReady,
} from '@screenly/edge-apps'
import { setupSentry } from '@screenly/edge-apps/utils'
import { inferSalesforceContentType, render } from './content'
import type { RenderContext } from './content'
import {
inferSalesforceContentType,
render,
type RenderContext,
} from './content'
import { createCredentialManager } from './credentials'

setupSentry('salesforce', {
Expand Down