Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no error statuses for failed requests (#18) #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions pages/api/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ interface ExtendedNextApiRequest extends NextApiRequest {
};
}

interface EndPointSuccess {
pass: string | number | null;
fail: string | number | null;
}
// Create a new ratelimiter, that allows 3 requests per 60 seconds
const ratelimit = redis
? new Ratelimit({
Expand All @@ -18,6 +22,14 @@ const ratelimit = redis
})
: undefined;

const endpointResponse = (restoredImage: string | null, endpointSuccess: EndPointSuccess) => {
const {pass, fail} = endpointSuccess;
if (restoredImage) {
return pass;
}
return fail;
}

export default async function handler(
req: ExtendedNextApiRequest,
res: NextApiResponse<Data>
Expand Down Expand Up @@ -79,7 +91,9 @@ export default async function handler(
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
const status = endpointResponse(restoredImage, {pass: 200, fail: 500})
const json = endpointResponse(restoredImage, {pass: restoredImage, fail: "Failed to restore image"})
res
.status(200)
Copy link
Author

Choose a reason for hiding this comment

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

The client checks the status of the request before rendering the restored image, when the JSON message is "Failed to restore image" while the status is 200, the client tries to render an image with the url of "Failed%20to%20restore%20image" returning a different status code fixes the issue from #18

.json(restoredImage ? restoredImage : "Failed to restore image");
.status(status)
.json(json);
}