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 all 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
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(e.stack);
} 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.error(e.stack);
} 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(e.stack);
} 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(e.stack);
} 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(e.stack);
} 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(e.stack);
} else {
throw e;
}
}
```

Expand Down
37 changes: 25 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,28 @@ 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 class WechatError extends Error {
constructor(resp) {
const message = resp.errStr || resp.errCode.toString();
super(message);
this.name = 'WechatError';
this.code = resp.errCode;

// avoid babel's limition about extending Error class
// https://github.com/babel/babel/issues/3083
Object.setPrototypeOf(this, WechatError.prototype);
}
}