Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit eb53640

Browse files
committed
move error handling to PastebinClient#post()
1 parent 2ec72df commit eb53640

File tree

5 files changed

+41
-80
lines changed

5 files changed

+41
-80
lines changed

src/struct/Paste.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,12 @@ module.exports = class Paste {
113113
throw new PastebinError("API key is required to delete a paste.")
114114
if (!this.client.credentials.userKey)
115115
throw new PastebinError("User key is required to delete a paste.")
116-
const { error } = await this.client.constructor.post(this.client.constructor.POST_URL, {
116+
await this.client.constructor.post(this.client.constructor.POST_URL, {
117117
api_dev_key: this.client.credentials.apiKey,
118118
api_user_key: this.client.credentials.userKey,
119119
api_option: "delete",
120120
api_paste_key: this.key
121121
})
122-
if (error) switch (error) {
123-
case "invalid api_dev_key":
124-
throw new PastebinError("Invalid API key.")
125-
case "invalid api_user_key":
126-
throw new PastebinError("Invalid user key.")
127-
case "invalid permission to remove paste":
128-
throw new PastebinError("No permission to delete this paste.")
129-
default:
130-
throw new PastebinError(`Unknown error: ${error}.`)
131-
}
132122
this.deleted = true
133123
return this
134124
}

src/struct/PastebinClient.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,40 @@ module.exports = class PastebinClient {
7373
const body = await response.text()
7474
if (body.toLowerCase().startsWith("bad api request")) {
7575
const error = body.slice("bad api request, ".length).toLowerCase()
76-
return { error, body: null }
76+
switch(error) {
77+
case "invalid login":
78+
throw new PastebinError("Invalid username or password.")
79+
case "account not active":
80+
throw new PastebinError("Account not active.")
81+
case "invalid api_dev_key":
82+
throw new PastebinError("Invalid API key.")
83+
case "invalid api_user_key":
84+
throw new PastebinError("Invalid user key.")
85+
case "invalid permission to view this paste or invalid api_paste_key":
86+
throw new PastebinError("The paste is private, or it doesn't exist.")
87+
case "invalid permission to remove paste":
88+
throw new PastebinError("No permission to delete this paste.")
89+
case "ip blocked":
90+
throw new PastebinError("IP blocked.")
91+
case "maximum number of 25 unlisted pastes for your free account":
92+
throw new PastebinError("Maximum of 25 unlisted pastes for free account exceeded.")
93+
case "maximum number of 10 private pastes for your free account":
94+
throw new PastebinError("Maximum of 10 private pastes for free account exceeded.")
95+
case "api_paste_code was empty":
96+
throw new PastebinError("Paste content is empty.")
97+
case "maximum paste file size exceeded":
98+
throw new PastebinError("Paste content exceeds maximum length.")
99+
case "invalid api_expire_date":
100+
throw new PastebinError("Invalid paste expiry.")
101+
case "invalid api_paste_private":
102+
throw new PastebinError("Invalid paste privacy setting.")
103+
case "invalid api_paste_format":
104+
throw new PastebinError("Invalid paste format.")
105+
case "invalid or expired api_user_key":
106+
throw new PastebinError("Invalid or expired user key.")
107+
default:
108+
throw new PastebinError(`Unknown error: ${error}.`)
109+
}
77110
} else {
78111
return { body, error: null }
79112
}
@@ -89,22 +122,12 @@ module.exports = class PastebinClient {
89122
if (!this.credentials.username || !this.credentials.password)
90123
throw new PastebinError("Username and password are required to login.")
91124

92-
const { error, body } = await this.constructor.post(this.constructor.LOGIN_URL, {
125+
const body = await this.constructor.post(this.constructor.LOGIN_URL, {
93126
api_dev_key: this.credentials.apiKey,
94127
api_user_name: this.credentials.username,
95128
api_user_password: this.credentials.password
96129
})
97130

98-
if (error) switch (error) {
99-
case "invalid api_dev_key":
100-
throw new PastebinError("Invalid API key.")
101-
case "invalid login":
102-
throw new PastebinError("Invalid username or password.")
103-
case "account not active":
104-
throw new PastebinError("Account not active.")
105-
default:
106-
throw new PastebinError(`Unknown error: ${error}.`)
107-
}
108131
this.credentials.userKey = body.trim()
109132
this.user = await this.users.fetch(this.credentials.username)
110133

src/struct/stores/PasteStore.js

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,12 @@ module.exports = class PasteStore extends Map {
2727
*/
2828
async fetch(key) {
2929
if (this.client.credentials.userKey) {
30-
const { error, body } = await this.client.constructor.post(this.client.constructor.RAW_URL, {
30+
const body = await this.client.constructor.post(this.client.constructor.RAW_URL, {
3131
api_dev_key: this.client.credentials.apiKey,
3232
api_user_key: this.client.credentials.userKey,
3333
api_paste_key: key,
3434
api_option: "show_paste"
3535
})
36-
if (error) switch (error) {
37-
case "invalid api_dev_key":
38-
throw new PastebinError("Invalid API key.")
39-
case "invalid api_user_key":
40-
throw new PastebinError("Invalid user key.")
41-
case "invalid permission to view this paste or invalid api_paste_key":
42-
throw new PastebinError("The paste is private, or it doesn't exist.")
43-
default:
44-
throw new PastebinError(`Unknown error: ${error}.`)
45-
}
4636
const paste = new Paste(this.client, { key, content: body })
4737
return paste
4838
} else {
@@ -96,34 +86,8 @@ module.exports = class PasteStore extends Map {
9686
if (privacy) requestBody.api_paste_private = privacy
9787
if (expiry) requestBody.api_paste_expire_date = expiry
9888

99-
const { error, body } = await this.client.constructor.post(this.client.constructor.POST_URL, requestBody)
89+
const body = await this.client.constructor.post(this.client.constructor.POST_URL, requestBody)
10090

101-
if (error) switch (error) {
102-
case "invalid api_dev_key":
103-
throw new PastebinError("Invalid API key.")
104-
case "ip blocked":
105-
throw new PastebinError("IP blocked.")
106-
case "maximum number of 25 unlisted pastes for your free account":
107-
throw new PastebinError("Maximum of 25 unlisted pastes for free account exceeded.")
108-
case "maximum number of 10 private pastes for your free account":
109-
throw new PastebinError("Maximum of 10 private pastes for free account exceeded.")
110-
case "api_paste_code was empty":
111-
throw new PastebinError("Paste content is empty.")
112-
case "maximum paste file size exceeded":
113-
throw new PastebinError("Paste content exceeds maximum length.")
114-
case "invalid api_expire_date":
115-
throw new PastebinError("Invalid paste expiry.")
116-
case "invalid api_paste_private":
117-
throw new PastebinError("Invalid paste privacy setting.")
118-
case "invalid api_paste_format":
119-
throw new PastebinError("Invalid paste format.")
120-
case "invalid api_user_key":
121-
throw new PastebinError("Invalid user key.")
122-
case "invalid or expired api_user_key":
123-
throw new PastebinError("Invalid or expired user key.")
124-
default:
125-
throw new PastebinError(`Unknown error: ${error}.`)
126-
}
12791
if (body.toLowerCase() === "post limit, maximum pastes per 24h reached")
12892
throw new PastebinError("Post limit per 24 hours exceeded.")
12993
const key = (body.match(/https?:\/\/pastebin\.com\/(.+)/i) || [])[1]

src/struct/stores/UserPasteStore.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,13 @@ module.exports = class UserPasteStore extends Map {
3939
throw new PastebinError("API key is required to fetch a user.")
4040
if (!this.client.credentials.userKey)
4141
throw new PastebinError("User key is required to fetch a user.")
42-
const { error, body } = await this.client.constructor.post(this.client.constructor.POST_URL, {
42+
const body = await this.client.constructor.post(this.client.constructor.POST_URL, {
4343
api_dev_key: this.client.credentials.apiKey,
4444
api_user_key: this.client.credentials.userKey,
4545
api_results_limit: max,
4646
api_option: "list"
4747
})
48-
if (error) switch (error) {
49-
case "invalid api_dev_key":
50-
throw new PastebinError("Invalid API key.")
51-
case "invalid api_user_key":
52-
throw new PastebinError("Invalid user key.")
53-
default:
54-
throw new PastebinError(`Unknown error: ${error}.`)
55-
}
56-
if (body.toLowerCase() === "no posts found.") return
48+
if (body.toLowerCase() === "no posts found.") return this
5749
const allPasteXMLs = body.match(/<paste>[\s\S]*?<\/paste>/g)
5850
if (!allPasteXMLs) return
5951
allPasteXMLs.forEach(async xml => {

src/struct/stores/UserStore.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,11 @@ module.exports = class UserStore extends Map {
3030
if (!this.client.credentials.userKey)
3131
throw new PastebinError("User key is required to fetch a user.")
3232

33-
const { body, error } = await this.client.constructor.post(this.client.constructor.POST_URL, {
33+
const body = await this.client.constructor.post(this.client.constructor.POST_URL, {
3434
api_dev_key: this.client.credentials.apiKey,
3535
api_user_key: this.client.credentials.userKey,
3636
api_option: "userdetails"
3737
})
38-
if (error) switch (error) {
39-
case "invalid api_dev_key":
40-
throw new PastebinError("Invalid API key.")
41-
case "invalid api_user_key":
42-
throw new PastebinError("Invalid user key.")
43-
default:
44-
throw new PastebinError(`Unknown error: ${error}.`)
45-
}
4638

4739
let invalidResponse
4840
const parsed = (await xml2js(body).catch(() => invalidResponse = true)).user

0 commit comments

Comments
 (0)