Closed counselorbot[bot] closed 2 years ago
🛑 There was an error: You missed something. What if someone didn't submit an image in the body? 🛑
Go ahead and merge this branch to main
to move on. Great work finishing this section!
⚠️ If you receive a
Conflicts
error, simply pressResolve conflicts
and you should be good to merge!
Week 3 Step 3 ⬤⬤⬤◯◯◯◯◯◯ | 🕐 Estimated completion: 10-20 minutes
Codename Blob + 3RR0R Handling
This week, you will be going through steps to handle POST requests with no data.
✅ Task:
codename
and uses it to name the image.uploadFile()
functionbunnimage/index.js
to thebunnimage
branch.bunnimage
ontomain
, and only merge the pull request when the bot approves your changes!🚧 Test Your Work
To test your work, use Postman to send a POST request without an image attached. You should see a response similar to the below:
1: Modify the Function
Handle Empty Data
Now we'll want to handle cases where the POST request's body happens to be empty. In your original
module.exports
function, before you parse the body, make sure it exists! Only then should you upload your file to blob storage.❓ How do I catch empty POST requests?
Use an try-catch statement to catch when `parse-multipart` is unable to parse the empty body. If catches an error, set the `responseMessage` to "Sorry! No image attached." Otherwise, you can safely parse the body! ```js let responseMessage = "" try { let password = // get the header called "codename" // use parse-multipart to parse the body // determine the file-type here! responseMessage = await uploadFile((place your parsedBody here), (place the extension here), (place the "codename" here)); // fill the parameters in! } catch(err) { context.log("Undefined body image"); responseMessage = "Sorry! No image attached." } ``` > 💡 Hint: `responseMessage` is what we're returning to the user as the output.