1

I am trying to fetch images from google cloud storage bucket from browser and serving the files using API in express. Following code breaks when the Image path is invalid. The try catch doesn't catch the file not found error. Am I missing something here? This code works if an image file exists.

From the Express side, I am using wildcard route (i.e. /*) as the API can accept any image path coming in and try to serve it.

const express = require('express')
const {Storage} = require('@google-cloud/storage');
let server = express()
const storage = new Storage();
const bucketName = '<some-bucket-name>'

server.get('/*', async (req, res) => {
  const widthString = req.query.width
  const heightString = req.query.height
  const format = req.query.format
  const fileName = req.path.substring(1);
  console.log("url: ", req.path)
  let width, height
  if (widthString) {
    width = parseInt(widthString)
  }
  if (heightString) {
    height = parseInt(heightString)
  }
  res.type(`image/${format || 'png'}`)
  
  try{
      await storage.bucket(bucketName).file(fileName).createReadStream().pipe(res)
  } catch (err) {
     console.error(err);
  }
})

Error:

url:  /media/artist/banner_image/16118/screenshot_2019_12_16_at_10.35.24_am.png
events.js:377
      throw er; // Unhandled 'error' event
      ^

ApiError: No such object: assets/media/artist/banner_image/16118/screenshot_2019_12_16_at_10.35.24_am.png
    at new ApiError (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/common/build/src/util.js:73:15)
    at Util.parseHttpRespMessage (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/common/build/src/util.js:175:41)
    at Util.handleResp (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/common/build/src/util.js:149:76)
    at Duplexify.<anonymous> (/home/ubuntu/imageoptimizer/node_modules/@google-cloud/storage/build/src/file.js:888:31)
    at Duplexify.emit (events.js:400:28)
    at PassThrough.emit (events.js:400:28)
    at onResponse (/home/ubuntu/imageoptimizer/node_modules/retry-request/index.js:222:19)
    at PassThrough.<anonymous> (/home/ubuntu/imageoptimizer/node_modules/retry-request/index.js:163:11)
    at PassThrough.emit (events.js:412:35)
    at /home/ubuntu/imageoptimizer/node_modules/teeny-request/build/src/index.js:191:27
Emitted 'error' event on PassThrough instance at:
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  code: 404,
Aditya Mertia
  • 495
  • 7
  • 19
  • You can look for the object before you download it. You can check [this article](https://cloud.google.com/storage/docs/listing-objects#code-samples) to do that. Or you can modify your code a bit like in [this sample](https://cloud.google.com/appengine/docs/flexible/nodejs/runtime) as it uses async function for download and catch the exception for the whole function along with the error minimizing in your error handler. – Rajeev Tirumalasetty Oct 22 '21 at 09:34
  • @RajeevTirumalasetty Listing object won't help, may be a validate object would have helped. The other sample mentioned by you is a nodejs runtime. Not sure how will it help here. – Aditya Mertia Oct 22 '21 at 12:00
  • You have to handle this exception by yourself. GCP won't throw the error directly. It only returns 404 as an output, and you have to handle it manually rather than using try catch. Or you can request it as a new feature in [issue tracker](https://cloud.google.com/support/docs/issue-trackers) however I am not sure how long it will take for the Google to implement this feature. – Rajeev Tirumalasetty Jan 13 '22 at 09:36

1 Answers1

0

Posting my previous comment as an answer for visibility

You have to handle this exception by yourself. GCP won't throw the error directly. It only returns 404 as an output, and you have to handle it manually rather than expecting try{} catch () {} to catch this exception. Or you can request it as a new feature in issue tracker, however I am not sure how long it will take for the Google to implement this feature.