Skip to content

Commit

Permalink
🚑 fix: handle error when upscaling image
Browse files Browse the repository at this point in the history
This commit handles errors that may occur when upscaling an image using the Pixelcut API.

Specifically, it:
- Adds a try/catch block to the image upscaling request.
- Parses the error response from the API and displays the error code and message to the user.
- Resets the reaction to "failed" if the upscaling request fails.
  • Loading branch information
binsarjr committed Mar 21, 2024
1 parent 3efc28f commit 099a91e
Showing 1 changed file with 49 additions and 27 deletions.
76 changes: 49 additions & 27 deletions src/actions/message/random/ResolveToHdAction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { WAMessage, WASocket } from "@whiskeysockets/baileys";
import FormData from "form-data";
import got from "got";
import got, { RequestError } from "got";
import BaseMessageHandlerAction from "../../../foundation/actions/BaseMessageHandlerAction.js";
import { Queue } from "../../../services/queue.js";
import { withSign } from "../../../supports/flag.js";
Expand Down Expand Up @@ -44,6 +44,7 @@ export default class extends BaseMessageHandlerAction {
directPath = message!.imageMessage!.directPath;
url = message!.imageMessage!.url;
} else {
this.resetReact(socket, _message);
return;
}

Expand All @@ -56,34 +57,55 @@ export default class extends BaseMessageHandlerAction {
data.append("image", photoBuffer, "image.jpg");
data.append("scale", "4");

const result = await got
.post("https://api2.pixelcut.app/image/upscale/v1", {
body: data,
headers: {
accept: "application/json",
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" +
Date.now(),
},
})
.json<{ result_url: string }>();
try {
const result = await got
.post("https://api2.pixelcut.app/image/upscale/v1", {
body: data,
headers: {
accept: "application/json",
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" +
Date.now(),
},
})
.json<{ result_url: string }>();

Queue.add(async () => {
if (!result.result_url) {
this.reactToFailed(socket, _message);
return;
}
Queue.add(async () => {
if (!result.result_url) {
this.reactToFailed(socket, _message);

await socket.sendMessage(
getJid(_message),
{
image: {
url: result.result_url,
return;
}

await socket.sendMessage(
getJid(_message),
{
image: {
url: result.result_url,
},
},
{ quoted: _message }
);
await this.reactToDone(socket, _message);
});
} catch (err) {
if (err instanceof RequestError) {
const { error, error_code } = JSON.parse(err.response!.body) as {
error: string;
error_code: string;
};
await socket.sendMessage(
getJid(_message),
{
text: `Error Code ${error_code}: ${error}`,
},
},
{ quoted: _message }
);
await this.reactToDone(socket, _message);
});
{ quoted: _message }
);
await this.reactToFailed(socket, _message);

return;
}
throw err;
}
}
}

0 comments on commit 099a91e

Please sign in to comment.