Use 'enums' for error codes
All checks were successful
CI / CI (push) Successful in 1m22s

This commit is contained in:
SileNce5k 2025-05-10 15:21:46 +02:00
parent 6a0350f13c
commit 787bfb43d4
Signed by: SileNce
GPG key ID: B0A142BB4291B204

View file

@ -46,12 +46,12 @@ module.exports = {
// TODO: Download with correct extension.
message.channel.sendTyping();
let downloadResult = await this.downloadImage(url, path.resolve(directory, "input.png"));
if(downloadResult.value != 0){
if(downloadResult.value === 3){
if(downloadResult.value != this.ERROR_CODES.SUCCESS){
if(downloadResult.value === this.ERROR_CODES.FETCH_ERROR){
message.channel.send(`Failed to download the provided image, got error '${downloadResult.errorMessage}'`);
}else if (downloadResult.value === 1){
}else if (downloadResult.value === this.ERROR_CODES.HTTP_ERROR){
message.channel.send(`Failed to download the provided image, got response status '${downloadResult.errorMessage}'`);
}else if(downloadResult.value === 2){
}else if(downloadResult.value === this.ERROR_CODES.NOT_IMAGE){
message.channel.send(`The provided url was not an image.`)
}
fs.rmSync(`${directory}`, {recursive: true})
@ -90,7 +90,7 @@ module.exports = {
},
// https://stackoverflow.com/a/77210219
async downloadImage(url, path) {
let res = new Response()
let res;
try {
res = await fetch(url);
} catch (error) {
@ -102,6 +102,12 @@ module.exports = {
const stream = Readable.fromWeb(res.body)
await writeFile(path, stream);
return {value: 0, errorMessage: ""};
},
ERROR_CODES: {
SUCCESS: 0,
HTTP_ERROR: 1,
NOT_IMAGE: 2,
FETCH_ERROR: 3
}
}