Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

fix #217 . reject promise with an error object instead of errCode number #222

Merged
merged 6 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 30 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ try {
});
console.log('share text message to time line successful:', result);
} catch (e) {
console.error('share text message to time line failed with:', e);
if (e instanceof WeChat.WechatError) {
console.error('share text message to time line failed with:', e.code);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里最好还是console.error(e.stack)比较好,这样更利于排错,另外message可以放在new WechatError()里去设置。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于 message,是要这样子吗:

new WechatError(resp.errCode, resp.errStr)

function WechatError(code, message) {
  this.code = code
  this.message = message
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实应该是:

class WechatError extends Error {
  constructor(code, message) {
    super(message);
    this.code = code;
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我测试了一下,使用 class WechatError extends Error 这种方式的话,e instanceOf WechatError 会返回 false,这样使用者就没办法正确判断 error 对象的类型了

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instanceof操作符是判断某个对象是否是从某个类或者某个类的父类中new出来的,所以按照class-extends的写法,e instanceof WechatError肯定是返回true的。

所以是否可以按照class-extends的写法提交一次代码,我可以帮你检查下问题 :)

Copy link
Contributor Author

@anjianshi anjianshi Jun 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新代码提交上来了

const e = new WechatError({ errCode: -1 });
e instanceof Error    // true
e instanceof WechatError  // false 

在 Chrome 里运行,e instanceof WechatError 是返回 true 的;但在 react-native 里就是返回 false;在 react-native 里用 js remote debug 运行,也是返回 false

} else {
throw e;
}
}

// Code example to share image url:
Expand All @@ -315,7 +319,11 @@ try {
});
console.log('share image url to time line successful:', result);
} catch (e) {
console.log('share image url to time line failed with:', e);
if (e instanceof WeChat.WechatError) {
console.log('share image url to time line failed with:', e.code);
} else {
throw e;
}
}

// Code example to share image file:
Expand Down Expand Up @@ -343,7 +351,11 @@ try {
});
console.log('share image file to time line successful:', result);
} catch (e) {
console.error('share image file to time line failed with:', e);
if (e instanceof WeChat.WechatError) {
console.error('share image file to time line failed with:', e.code);
} else {
throw e;
}
}

// Code example to share image resource:
Expand All @@ -361,7 +373,11 @@ try {
console.log('share resource image to time line successful', result);
}
catch (e) {
console.error('share resource image to time line failed', e);
if (e instanceof WeChat.WechatError) {
console.error('share resource image to time line failed', e.code);
} else {
throw e;
}
}

// Code example to download an word file from web, then share it to WeChat session
Expand Down Expand Up @@ -389,7 +405,11 @@ try {
});
console.log('share word file to chat session successful', result);
} catch (e) {
console.error('share word file to chat session failed', e);
if (e instanceof WeChat.WechatError) {
console.error('share word file to chat session failed', e.code);
} else {
throw e;
}
}

//android code use ExternalDirectoryPath
Expand All @@ -415,7 +435,11 @@ try {
console.log('share word file to chat session successful', result);
}
catch (e) {
console.error('share word file to chat session failed', e);
if (e instanceof WeChat.WechatError) {
console.error('share word file to chat session failed', e.code);
} else {
throw e;
}
}
```

Expand Down
29 changes: 17 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,10 @@ export function sendAuthRequest(scopes, state) {
return new Promise((resolve, reject) => {
WeChat.sendAuthRequest(scopes, state, () => {});
emitter.once('SendAuth.Resp', resp => {
const result = resp.errCode;
if (result === 0) {
if (resp.errCode === 0) {
resolve(resp);
} else {
reject(result);
reject(new WechatError(resp));
}
});
});
Expand All @@ -181,11 +180,10 @@ export function shareToTimeline(data) {
return new Promise((resolve, reject) => {
nativeShareToTimeline(data);
emitter.once('SendMessageToWX.Resp', resp => {
const result = resp.errCode;
if (result === 0) {
if (resp.errCode === 0) {
resolve(resp);
} else {
reject(result);
reject(new WechatError(resp));
}
});
});
Expand All @@ -208,11 +206,10 @@ export function shareToSession(data) {
return new Promise((resolve, reject) => {
nativeShareToSession(data);
emitter.once('SendMessageToWX.Resp', resp => {
const result = resp.errCode;
if (result === 0) {
if (resp.errCode === 0) {
resolve(resp);
} else {
reject(result);
reject(new WechatError(resp));
}
});
});
Expand All @@ -235,12 +232,20 @@ export function pay(data) {
if (result) reject(result);
});
emitter.once('PayReq.Resp', resp => {
const result = resp.errCode;
if (result === 0) {
if (resp.errCode === 0) {
resolve(resp);
} else {
reject(result);
reject(new WechatError(resp));
}
});
});
}

/**
* promises will reject with this error when API call finish with an errCode other than zero.
*/
export function WechatError(apiResp) {
this.code = apiResp.errCode;
this.message = apiResp.errStr || apiResp.errCode.toString();
}
WechatError.prototype = Error.prototype;