Skip to content

Commit

Permalink
Merge pull request #37 from wechaty/add-error-class
Browse files Browse the repository at this point in the history
feat: add new class WAError and enum WAErrorType
  • Loading branch information
bung87 authored Jan 13, 2022
2 parents 42f8b75 + 1a0be38 commit 808d086
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 12 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"dependencies": {
"flash-store": "^0.12.7",
"fs-extra": "^8.1.0",
"memory-card": "^1.0.3",
"rxjs": "^7.5.1",
"wechaty-puppet": "^0.41.9",
"whatsapp-web.js": "^1.15.3"
Expand Down
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/// <reference path="./typings.d.ts" />
import { log, FileBox } from 'wechaty-puppet'
import {
FileBox,
log,
} from 'wechaty-puppet'
import { packageJson } from './package-json.js'

const VERSION = packageJson.version || '0.0.0'
Expand Down
8 changes: 5 additions & 3 deletions src/data-manager/cache-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as os from 'os'
import { FlashStore } from 'flash-store'
import type WhatsAppRaw from '../schema/index'
import { log } from '../config.js'
import WAError from '../pure-function-helpers/error-type.js'
import { WXWORK_ERROR_TYPE } from '../schema/error-type.js'

const PRE = 'CacheManager'

Expand All @@ -19,7 +21,7 @@ export class CacheManager {

public static get Instance () {
if (!this._instance) {
throw new Error('no instance')
throw new WAError(WXWORK_ERROR_TYPE.ERR_NO_CACHE, 'no instance')
}
return this._instance
}
Expand Down Expand Up @@ -70,7 +72,7 @@ export class CacheManager {

private getMessageCache () {
if (!this.cacheMessageRawPayload) {
throw new Error('getMessageCache() has no cache')
throw new WAError(WXWORK_ERROR_TYPE.ERR_NO_CACHE, 'getMessageCache() has no cache')
}
return this.cacheMessageRawPayload
}
Expand All @@ -86,7 +88,7 @@ export class CacheManager {
): Promise<void> {

if (this.cacheMessageRawPayload) {
throw new Error('cacheMessageRawPayload does not exist.')
throw new WAError(WXWORK_ERROR_TYPE.ERR_INIT, 'cacheMessageRawPayload does not exist.')
}

const baseDir = path.join(
Expand Down
22 changes: 15 additions & 7 deletions src/puppet-whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,32 @@
*
*/
import * as PUPPET from 'wechaty-puppet'
import { log, FileBox } from 'wechaty-puppet'
import type { MemoryCard } from 'memory-card'
import { distinctUntilKeyChanged, fromEvent, map, merge } from 'rxjs'
import {
distinctUntilKeyChanged,
fromEvent,
map,
merge,
} from 'rxjs'
import {
avatarForGroup,
log,
FileBox,
MEMORY_SLOT,
VERSION,
} from './config.js'
} from './config.js'

import {
getWhatsApp,
WhatsApp,
WhatsappContact,
WhatsappMessage,
} from './whatsapp.js'
import WAWebJS, { ClientOptions, GroupChat, MessageContent, MessageMedia, MessageTypes } from 'whatsapp-web.js'
import WAWebJS, { ClientOptions, GroupChat, MessageContent } from 'whatsapp-web.js'
import { parseVcard } from './pure-function-helpers/vcard-parser.js'
import { Manager } from './work/manager.js'
import WAError from './pure-function-helpers/error-type.js'
import { WXWORK_ERROR_TYPE } from './schema/error-type.js'
// @ts-ignore
// import { MessageTypes } from 'whatsapp-web.js'
// import { Attachment } from './mock/user/types'
Expand Down Expand Up @@ -443,7 +451,7 @@ class PuppetWhatsapp extends PUPPET.Puppet {
log.error('Message %s not found', messageId)
throw new Error('Message not found')
}
if (msg.type !== MessageTypes.CONTACT_CARD) {
if (msg.type !== WAWebJS.MessageTypes.CONTACT_CARD) {
log.error('Message %s is not contact type', messageId)
throw new Error('Message is not contact type')
}
Expand Down Expand Up @@ -564,7 +572,7 @@ class PuppetWhatsapp extends PUPPET.Puppet {

override async messageSendFile (conversationId: string, file: FileBox): Promise<void> {
log.verbose('PuppetWhatsApp', 'messageSendFile(%s, %s)', conversationId, file.name)
const msgContent = new MessageMedia(file.mimeType!, await file.toBase64(), file.name)
const msgContent = new WAWebJS.MessageMedia(file.mimeType!, await file.toBase64(), file.name)
return this.messageSend(conversationId, msgContent)
}

Expand Down Expand Up @@ -737,7 +745,7 @@ class PuppetWhatsapp extends PUPPET.Puppet {
if (group) {
return group.gid
} else {
throw new Error('An error occurred while creating the group!')
throw new WAError(WXWORK_ERROR_TYPE.ERR_CREATE_ROOM, 'An error occurred while creating the group!')
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/pure-function-helpers/error-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { WAErrorType } from '../schema/error-type'

export default class WAError extends Error {

constructor (type: WAErrorType, message: string) {
super(`${type} ${message}`)
}

}
44 changes: 44 additions & 0 deletions src/schema/error-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
enum BASE_ERROR_TYPE {
ERR_REQUEST_TIMEOUT = 'ERR_REQUEST_TIMEOUT', // 请求超时
ERR_START = 'ERR_START', // 启动异常
ERR_STOP = 'ERR_STOP', // 停止异常
}

enum INIT_ERROR_TYPE {
ERR_INIT = 'ERR_INIT', // 未初始化
ERR_NOT_LOGIN = 'ERR_NOT_LOGIN', // 未登录
ERR_CACHE_EXISTED = 'ERR_CACHE_EXISTED', // 缓存已存在
ERR_NO_CACHE = 'ERR_NO_CACHE', // 缓存无效
}

enum MESSAGE_ERROR_TYPE {
ERR_SEND_MSG = 'ERR_SEND_MSG', // 发送消息失败
ERR_SEND_MSG_TIMEOUT = 'ERR_SEND_MSG_TIMEOUT', // 发送消息超时
ERR_UNKNOWN_SEND_STATUS = 'ERR_UNKNOWN_SEND_STATUS', // 未知消息发送结果
}

enum ROOM_ERROR_TYPE {
ERR_ROOM_NOT_FOUND = 'ERR_ROOM_NOT_FOUND', // 群聊不存在
ERR_CREATE_ROOM = 'ERR_CREATE_ROOM', // 创建群聊失败
ERR_MODIFY_ROOM_NAME = 'ERR_MODIFY_ROOM_NAME', // 修改群名称失败
ERR_ADD_ROOM = 'ERR_ADD_ROOM', // 拉人进群失败
ERR_REMOVE_ROOM = 'ERR_REMOVE_ROOM', // 踢人出群失败
ERR_ACCEPT_ROOM_INVITATION = 'ERR_ACCEPT_ROOM_INVITATION', // 自动通过群邀请失败
ERR_ANNOUNCE_NO_PERMISSION = 'ERR_ANNOUNCE_NO_PERMISSION', // 无发送群公告权限
}

enum CONTACT_ERROR_TYPE {
ERR_CONTACT_NOT_FOUND = 'ERR_CONTACT_NOT_FOUND', // 联系人不存在
ERR_INVALID_CONTACT_ID = 'ERR_INVALID_CONTACT_ID', // 联系人ID无效
ERR_CONTACT_CARD_ID = 'ERR_CONTACT_CARD_ID', // 名片id异常
}

export const WXWORK_ERROR_TYPE = {
...BASE_ERROR_TYPE, // 基础错误类型
...INIT_ERROR_TYPE, // 初始化错误类型
...MESSAGE_ERROR_TYPE, // 消息相关错误类型
...ROOM_ERROR_TYPE, // 群相关错误类型
...CONTACT_ERROR_TYPE, // 联系人相关错误类型
}

export type WAErrorType = BASE_ERROR_TYPE | INIT_ERROR_TYPE | MESSAGE_ERROR_TYPE | ROOM_ERROR_TYPE | CONTACT_ERROR_TYPE

0 comments on commit 808d086

Please sign in to comment.