File size: 1,678 Bytes
8a29801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type {ReportSubmitResult} from "../facades/reports"

export type AutomaticReportSubmissionStatus =
  | {status: "filed"; reportId: string}
  | {status: "skipped"; reason: string}
  | {status: "failed"; error: string}

export function toAutomaticReportSubmissionStatus(result: ReportSubmitResult): AutomaticReportSubmissionStatus {
  if (result.status === "submitted") {
    return {status: "filed", reportId: result.reportId}
  }
  if (result.status === "skipped") {
    return {status: "skipped", reason: result.reason}
  }
  return {status: "failed", error: result.error}
}

export function automaticReportErrorStatus(error: unknown): Extract<AutomaticReportSubmissionStatus, {status: "failed"}> {
  return {status: "failed", error: error instanceof Error ? error.message : String(error)}
}

export function logAutomaticReportSubmissionStatus(

  logTag: string,

  result: AutomaticReportSubmissionStatus,

  ...throttleDetails: unknown[]

): void {
  if (result.status === "filed") {
    console.log(`[${logTag}] Report filed:`, result.reportId)
    return
  }
  if (result.status === "skipped") {
    console.log(
      `[${logTag}] Skipping automatic report within throttle window:`,
      ...(throttleDetails.length > 0 ? throttleDetails : [result.reason]),
    )
    return
  }
  console.error(`[${logTag}] submit failed:`, result.error)
}

export function logUnexpectedAutomaticReportError(

  logTag: string,

  error: unknown,

): Extract<AutomaticReportSubmissionStatus, {status: "failed"}> {
  const result = automaticReportErrorStatus(error)
  console.error(`[${logTag}] Unexpected error:`, error)
  return result
}