Skip to content

Commit

Permalink
fix(performance): inconsistent handling when trace does not exist (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebarooni authored Jan 11, 2025
1 parent ea16204 commit 73f3ca4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-emus-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/performance': major
---

fix: throw error on web when trace is not found
7 changes: 7 additions & 0 deletions packages/performance/BREAKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ This is a comprehensive list of the breaking changes introduced in the major ver

## Versions

- [Version 7.x.x](#version-7xx)
- [Version 6.x.x](#version-6xx)
- [Version 5.x.x](#version-5xx)
- [Version 1.x.x](#version-1xx)

## Version 7.x.x

### Error Handling

The web implementation now throws an error when a `trace` is not found for a given `traceName`. This behavior was already in place on iOS and Android in earlier versions.

## Version 6.x.x

### Dependencies
Expand Down
18 changes: 10 additions & 8 deletions packages/performance/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class FirebasePerformanceWeb
extends WebPlugin
implements FirebasePerformancePlugin
{
private static readonly ERROR_TRACE_NOT_FOUND =
'No trace was found with the provided traceName.';
private traces: { [key: string]: PerformanceTrace | undefined } = {};

public async startTrace(options: StartTraceOptions): Promise<void> {
Expand All @@ -37,7 +39,7 @@ export class FirebasePerformanceWeb
public async stopTrace(options: StopTraceOptions): Promise<void> {
const trace = this.traces[options.traceName];
if (!trace) {
return;
throw new Error(FirebasePerformanceWeb.ERROR_TRACE_NOT_FOUND);
}
trace.stop();
delete this.traces[options.traceName];
Expand All @@ -46,7 +48,7 @@ export class FirebasePerformanceWeb
public async incrementMetric(options: IncrementMetricOptions): Promise<void> {
const trace = this.traces[options.traceName];
if (!trace) {
return;
throw new Error(FirebasePerformanceWeb.ERROR_TRACE_NOT_FOUND);
}
trace.incrementMetric(options.metricName, options.incrementBy);
}
Expand All @@ -72,7 +74,7 @@ export class FirebasePerformanceWeb
}: PutAttributeOptions): Promise<void> {
const trace = this.traces[traceName];
if (!trace) {
return;
throw new Error(FirebasePerformanceWeb.ERROR_TRACE_NOT_FOUND);
}
trace.putAttribute(attribute, value);
return;
Expand All @@ -84,7 +86,7 @@ export class FirebasePerformanceWeb
}: GetAttributeOptions): Promise<GetAttributeResult> {
const trace = this.traces[traceName];
if (!trace) {
return { value: null };
throw new Error(FirebasePerformanceWeb.ERROR_TRACE_NOT_FOUND);
}
return { value: trace.getAttribute(attribute) ?? null };
}
Expand All @@ -94,7 +96,7 @@ export class FirebasePerformanceWeb
}: GetAttributesOptions): Promise<GetAttributesResult> {
const trace = this.traces[traceName];
if (!trace) {
return { attributes: {} };
throw new Error(FirebasePerformanceWeb.ERROR_TRACE_NOT_FOUND);
}
return { attributes: trace.getAttributes() };
}
Expand All @@ -105,7 +107,7 @@ export class FirebasePerformanceWeb
}: RemoveAttributeOptions): Promise<void> {
const trace = this.traces[traceName];
if (!trace) {
return;
throw new Error(FirebasePerformanceWeb.ERROR_TRACE_NOT_FOUND);
}
trace.removeAttribute(attribute);
}
Expand All @@ -117,7 +119,7 @@ export class FirebasePerformanceWeb
}: PutMetricOptions): Promise<void> {
const trace = this.traces[traceName];
if (!trace) {
return;
throw new Error(FirebasePerformanceWeb.ERROR_TRACE_NOT_FOUND);
}
trace.putMetric(metricName, num);
}
Expand All @@ -128,7 +130,7 @@ export class FirebasePerformanceWeb
}: GetMetricOptions): Promise<GetMetricResult> {
const trace = this.traces[traceName];
if (!trace) {
return { value: 0 };
throw new Error(FirebasePerformanceWeb.ERROR_TRACE_NOT_FOUND);
}
return { value: trace.getMetric(metricName) };
}
Expand Down

0 comments on commit 73f3ca4

Please sign in to comment.