From 92800c28782b22aced273c0d7109522addb21b65 Mon Sep 17 00:00:00 2001 From: lijiarui Date: Sat, 11 Mar 2017 17:03:18 +0800 Subject: [PATCH] #291 change `throw error` to `return null` (#292) * #291 change throw error to return null * add jsDoc --- src/contact.ts | 8 +++++--- src/room.ts | 9 +++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/contact.ts b/src/contact.ts index 2cd13572..8a9214a9 100644 --- a/src/contact.ts +++ b/src/contact.ts @@ -348,14 +348,16 @@ export class Contact implements Sayable { } /** - * try to find a contact by filter: {name: string | RegExp} + * try to find a contact by filter: {name: string | RegExp} / {alias: string | RegExp} + * @param {ContactQueryFilter} query + * @returns {Promise} If can find the contact, return Contact, or return null */ - public static async find(query: ContactQueryFilter): Promise { + public static async find(query: ContactQueryFilter): Promise { log.verbose('Contact', 'find(%s)', JSON.stringify(query)) const contactList = await Contact.findAll(query) if (!contactList || !contactList.length) { - throw new Error('find not found any contact') + return null } if (contactList.length > 1) { diff --git a/src/room.ts b/src/room.ts index 800c244d..9e84db08 100644 --- a/src/room.ts +++ b/src/room.ts @@ -498,12 +498,17 @@ export class Room extends EventEmitter implements Sayable { }) } - public static async find(query: RoomQueryFilter): Promise { + /** + * try to find a room by filter: {topic: string | RegExp} + * @param {RoomQueryFilter} query + * @returns {Promise} If can find the room, return Room, or return null + */ + public static async find(query: RoomQueryFilter): Promise { log.verbose('Room', 'find({ topic: %s })', query.topic) const roomList = await Room.findAll(query) if (!roomList || roomList.length < 1) { - throw new Error('no room found') + return null } const room = roomList[0] await room.ready() -- GitLab