Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new class WAError and enum WAErrorType #37

Merged
merged 6 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@
"wechaty-puppet": "^0.41.9"
},
"dependencies": {
"rxjs": "^7.5.1",
"file-box": "^1.4.12",
"flash-store": "^0.12.7",
"fs-extra": "^8.1.0",
"rxjs": "^7.5.1",
"whatsapp-web.js": "^1.15.3"
},
"tap": {
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 { WhatsAppMessagePayload } from '../schema/message'
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
18 changes: 13 additions & 5 deletions src/puppet-whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,30 @@
*
*/
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'
} from './whatsapp.js'
import WAWebJS, { ClientOptions, GroupChat } from 'whatsapp-web.js'
import WAError from './pure-function-helpers/error-type.js'
import { WXWORK_ERROR_TYPE } from './schema/error-type.js'
import { Manager } from './work/manager.js'
// @ts-ignore
// import { MessageTypes } from 'whatsapp-web.js'
Expand Down Expand Up @@ -697,7 +705,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