Skip to content

Commit

Permalink
Breaking change to ErrorService:
Browse files Browse the repository at this point in the history
- remove rollbar service
- ErrorService now passed in as a startOption, configuration passed to init function called on the service.
  • Loading branch information
ljowen committed Aug 29, 2024
1 parent ce08c31 commit ab1bb2f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 150 deletions.
28 changes: 4 additions & 24 deletions lib/Models/ErrorServiceProviders/ErrorService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TerriaError from "../../Core/TerriaError";
import SentryErrorServiceProvider from "./SentryErrorServiceProvider";
import { ConfigParameters } from "../Terria";
export interface ErrorServiceOptions {
provider: string;
configuration: any;
Expand All @@ -8,27 +8,7 @@ export interface ErrorServiceOptions {
/**
* The interface that all valid error service providers must implement.
*/
export type ErrorServiceProvider = {
error(error: string | Error | TerriaError): void;
};

/**
* Asynchronously loads and returns an error service provider instance for the given options.
*/
export async function initializeErrorServiceProvider(
options?: ErrorServiceOptions
): Promise<ErrorServiceProvider> {
const provider = options?.provider;
const configuration = options?.configuration;

if (provider === "rollbar") {
const rollbarModule = await import("./RollbarErrorServiceProvider");
const rollbarProvider = new rollbarModule.default(configuration);
return rollbarProvider;
} else if (provider === "sentry") {
const sentryProvider = new SentryErrorServiceProvider();
return sentryProvider;
} else {
throw new Error(`Unknown error service provider: ${provider}`);
}
export interface ErrorServiceProvider {
init: (config: ConfigParameters) => void;
error: (error: string | Error | TerriaError) => void;
}
39 changes: 0 additions & 39 deletions lib/Models/ErrorServiceProviders/RollbarErrorServiceProvider.ts

This file was deleted.

44 changes: 0 additions & 44 deletions lib/Models/ErrorServiceProviders/SentryErrorServiceProvider.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import { ErrorServiceProvider } from "./ErrorService";
* A stub error service provider that does nothing.
*/
export default class StubErrorServiceProvider implements ErrorServiceProvider {
init() {}
error(_error: string | Error | TerriaError) {}
}
38 changes: 15 additions & 23 deletions lib/Models/Terria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ import updateModelFromJson from "./Definition/updateModelFromJson";
import upsertModelFromJson from "./Definition/upsertModelFromJson";
import {
ErrorServiceOptions,
ErrorServiceProvider,
initializeErrorServiceProvider
ErrorServiceProvider
} from "./ErrorServiceProviders/ErrorService";
import StubErrorServiceProvider from "./ErrorServiceProviders/StubErrorServiceProvider";
import TerriaFeature from "./Feature/Feature";
Expand Down Expand Up @@ -360,6 +359,7 @@ interface StartOptions {
};
applicationUrl?: Location;
shareDataService?: ShareDataService;
errorService?: ErrorServiceProvider;
/**
* i18nOptions is explicitly a separate option from `languageConfiguration`,
* as `languageConfiguration` can be serialised, but `i18nOptions` may have
Expand Down Expand Up @@ -679,8 +679,8 @@ export default class Terria {
readonly developmentEnv = process?.env?.NODE_ENV === "development";

/**
* An error service instance. The instance can be configured by setting the
* `errorService` config parameter. Here we initialize it to stub provider so
* An error service instance. The instance can be provided via the
* `errorService` startOption. Here we initialize it to stub provider so
* that the `terria.errorService` always exists.
*/
errorService: ErrorServiceProvider = new StubErrorServiceProvider();
Expand Down Expand Up @@ -912,20 +912,6 @@ export default class Terria {
this.modelIdShareKeysMap.set(id, [shareKey]);
}

/**
* Initialize errorService from config parameters.
*/
setupErrorServiceProvider(errorService: ErrorServiceOptions) {
initializeErrorServiceProvider(errorService)
.then((errorService) => {
this.errorService = errorService;
console.log("error initd", errorService);
})
.catch((e) => {
console.error("Failed to initialize error service", e);
});
}

setupInitializationUrls(baseUri: uri.URI, config: any) {
const initializationUrls: string[] = config?.initializationUrls || [];
const initSources: InitSource[] = initializationUrls.map((url) => ({
Expand Down Expand Up @@ -1017,10 +1003,6 @@ export default class Terria {
if (isJsonObject(config) && isJsonObject(config.parameters)) {
this.updateParameters(config.parameters);
}

if (this.configParameters.errorService) {
this.setupErrorServiceProvider(this.configParameters.errorService);
}
this.setupInitializationUrls(baseUri, config);
});
} catch (error) {
Expand All @@ -1042,7 +1024,17 @@ export default class Terria {
setCustomRequestSchedulerDomainLimits(
this.configParameters.customRequestSchedulerLimits
);

if (options.errorService) {
try {
this.errorService = options.errorService;
this.errorService.init(this.configParameters);
} catch (e) {
console.error(
`Failed to initialize error service: ${this.configParameters.errorService?.provider}`,
e
);
}
}
this.analytics?.start(this.configParameters);
this.analytics?.logEvent(
Category.launch,
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@
"react-virtual": "~2.3.2",
"resolve-url-loader": "^5.0.0",
"retry": "^0.12.0",
"rollbar": "^2.24.0",
"sass-loader": "^10",
"shpjs": "^4.0.4",
"simple-statistics": "^7.0.1",
Expand Down
73 changes: 54 additions & 19 deletions test/Models/ErrorServiceSpec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,60 @@
import { initializeErrorServiceProvider } from "../../lib/Models/ErrorServiceProviders/ErrorService";
import RollbarErrorServiceProvider from "../../lib/Models/ErrorServiceProviders/RollbarErrorServiceProvider";
import TerriaError from "../../lib/Core/TerriaError";
import { ErrorServiceProvider } from "../../lib/Models/ErrorServiceProviders/ErrorService";
import StubErrorServiceProvider from "../../lib/Models/ErrorServiceProviders/StubErrorServiceProvider";
import Terria from "../../lib/Models/Terria";

describe("initializeErrorServiceProvider", function () {
it("can initialize RollbarErrorServiceProvider", async function () {
const errorService = await initializeErrorServiceProvider({
provider: "rollbar",
configuration: {}
describe("ErrorService", function () {
let mockErrorServiceProvider: ErrorServiceProvider;
let terria: Terria;

beforeAll(() => {
jasmine.Ajax.install();
jasmine.Ajax.stubRequest(/.*/).andError({});
jasmine.Ajax.stubRequest(/.*(serverconfig|proxyabledomains).*/).andReturn({
responseText: JSON.stringify({ foo: "bar" })
});
jasmine.Ajax.stubRequest("test-config.json").andReturn({
responseText: JSON.stringify({ config: true })
});
expect(errorService instanceof RollbarErrorServiceProvider).toBe(true);
mockErrorServiceProvider = {
init: () => {},
error: () => {}
};
});

it("throws an error when an invalid provider type is given", async function () {
let error;
try {
await initializeErrorServiceProvider({
provider: "foo",
configuration: undefined
});
} catch (e: any) {
error = e;
}
expect(error.message).toBe(`Unknown error service provider: foo`);
beforeEach(() => {
terria = new Terria({
appBaseHref: "/",
baseUrl: "./"
});
});

it("Initializes an error service, passing in config", async function () {
const initSpy = spyOn(mockErrorServiceProvider, "init");
await terria.start({
configUrl: "test-config.json",
errorService: mockErrorServiceProvider
});
expect(initSpy).toHaveBeenCalledTimes(1);
});

it("Gets called with error", async function () {
const errorSpy = spyOn(mockErrorServiceProvider, "error").and.callThrough();
await terria.start({
configUrl: "test-config.json",
errorService: mockErrorServiceProvider
});
const error = new TerriaError({
message: "test error"
});
terria.raiseErrorToUser(error);
expect(errorSpy).toHaveBeenCalledWith(error);
});

it("Falls back to stub provider", () => {
terria.start({
configUrl: "test-config.json"
});
expect(terria.errorService).toEqual(jasmine.any(StubErrorServiceProvider));
});
});

0 comments on commit ab1bb2f

Please sign in to comment.