Skip to content

Commit

Permalink
Add toThrowErrorCode
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Jul 9, 2024
1 parent 1511b61 commit fa26133
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,12 +783,16 @@ function toChangePack(pack: ChangePack<Indexable>): PbChangePack {
* `errorCodeOf` returns the error code of the given connect error.
*/
export function errorCodeOf(error: ConnectError): string {
// NOTE(hackerwins): Currently, we only use the first detail to represent the
// error code.
const infos = error.findDetails(ErrorInfo);
for (const info of infos) {
return info.metadata.code;
if (info.metadata.code) {
return info.metadata.code;
}
}

return "";
return '';
}

/**
Expand Down
41 changes: 34 additions & 7 deletions test/integration/client_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2024 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, it, assert, vi, afterEach, expect } from 'vitest';

import yorkie, {
Expand All @@ -15,7 +31,8 @@ import {
testRPCAddr,
withTwoClientsAndDocuments,
} from '@yorkie-js-sdk/test/integration/integration_helper';
import { ConnectError, Code } from '@connectrpc/connect';
import { ConnectError, Code as ConnectCode } from '@connectrpc/connect';
import { Code } from '@yorkie-js-sdk/src/util/error';

describe.sequential('Client', function () {
afterEach(() => {
Expand Down Expand Up @@ -49,12 +66,22 @@ describe.sequential('Client', function () {
it('Can attach/detach document', async function ({ task }) {
const cli = new yorkie.Client(testRPCAddr);
await cli.activate();

const key = toDocKey(`${task.name}-${new Date().getTime()}`);
const doc = new yorkie.Document<{ version: number }>(key);
const doc = new yorkie.Document(
toDocKey(`${task.name}-${new Date().getTime()}`),
);

await cli.attach(doc);
await expect(async () => cli.attach(doc)).rejects.toThrow('is not detached');
expect(async () => cli.attach(doc)).rejects.toThrowErrorCode(
Code.DocumentNotDetached,
);

await cli.detach(doc);
expect(async () => cli.detach(doc)).rejects.toThrowErrorCode(
Code.DocumentNotAttached,
);

await cli.deactivate();
await cli.deactivate();
});

it('Can handle sync', async function ({ task }) {
Expand Down Expand Up @@ -790,7 +817,7 @@ describe.sequential('Client', function () {

// 01. Simulate Unknown error.
vi.stubGlobal('fetch', async () => {
throw new ConnectError('Failed to fetch', Code.Unknown);
throw new ConnectError('Failed to fetch', ConnectCode.Unknown);
});

doc.update((root) => {
Expand All @@ -803,7 +830,7 @@ describe.sequential('Client', function () {

// 02. Simulate FailedPrecondition error.
vi.stubGlobal('fetch', async () => {
throw new ConnectError('Failed to fetch', Code.FailedPrecondition);
throw new ConnectError('Failed to fetch', ConnectCode.FailedPrecondition);
});

await new Promise((res) => setTimeout(res, 30));
Expand Down
27 changes: 27 additions & 0 deletions test/vitest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { Assertion, AsymmetricMatchersContaining } from 'vitest';

Check warning on line 17 in test/vitest.d.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'Assertion' is defined but never used

Check warning on line 17 in test/vitest.d.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'AsymmetricMatchersContaining' is defined but never used
import { Code } from '@yorkie-js-sdk/src/util/error';

interface CustomMatchers<R = unknown> {
toThrowErrorCode: (expected: Code) => R;
}

declare module 'vitest' {
interface Assertion<T = any> extends CustomMatchers<T> {}
interface AsymmetricMatchersContaining extends CustomMatchers {}

Check failure on line 26 in test/vitest.d.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

An interface declaring no members is equivalent to its supertype
}
29 changes: 29 additions & 0 deletions test/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from 'vitest';
import { Code } from '@yorkie-js-sdk/src/util/error';

expect.extend({
toThrowErrorCode(received: any, expected: Code) {
const { isNot } = this;
const pass = received?.code === expected;
return {
pass,
message: () => `${received} is${isNot ? ' not' : ''} expected`,
};
},
});
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default defineConfig({
benchmark: {
exclude: ['**/lib/**', '**/node_modules/**'],
},
setupFiles: ['./test/vitest.setup.ts'],
},
plugins: [
tsconfigPaths({
Expand Down

0 comments on commit fa26133

Please sign in to comment.