Skip to content

Commit

Permalink
feature(bbcode-dataprocessor): Provide a check if an error is a Conte…
Browse files Browse the repository at this point in the history
…xtMismatchError
  • Loading branch information
JensDallmann committed Oct 18, 2023
1 parent 1f4989f commit 80e4341
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContextMismatchError } from "../src/ContextMismatchError";
import { ContextMismatchError, isContextMismatchError } from "../src/ContextMismatchError";

describe("ContextMismatchError", () => {
it("should be possible to instantiate the error without a message", () => {
Expand Down Expand Up @@ -48,4 +48,12 @@ describe("ContextMismatchError", () => {
expect(error).toHaveProperty("actual", actual);
expect(error).toHaveProperty("expected", expected);
});

it("should be possible to check if an error is a ContextMismatchError", () => {
const error = new ContextMismatchError();
expect(isContextMismatchError(error)).toBe(true);

const anyError = new Error("This is any error and the typeguard should return false");
expect(isContextMismatchError(anyError)).toBe(false);
});
});
8 changes: 8 additions & 0 deletions packages/ckeditor5-data-facade/src/ContextMismatchError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ export class ContextMismatchError extends Error {
return `ContextMismatchError: ${this.message} {actual=${this.actual}, expected=${this.expected}}`;
}
}

/**
* Typeguard to check if an error is a `ContextMismatchError`.
*
* @param error - the error to check
*/
export const isContextMismatchError = (error: unknown): error is ContextMismatchError =>
error instanceof ContextMismatchError;
3 changes: 2 additions & 1 deletion packages/ckeditor5-data-facade/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module ckeditor5-data-facade
*/

export { ContextMismatchError } from "./ContextMismatchError";
export { ContextMismatchError, isContextMismatchError } from "./ContextMismatchError";

export type { DataApi, GetDataApi, SetDataApi } from "./DataApi";

Expand All @@ -25,4 +25,5 @@ export type { DataFacadeConfig, Save } from "./DataFacadeConfig";

export { DataFacadeController } from "./DataFacadeController";


Check failure on line 28 in packages/ckeditor5-data-facade/src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Delete `⏎`
import "./augmentation";

0 comments on commit 80e4341

Please sign in to comment.