Sharp: Resizing an image file that doesn't exist

Created on 7 Feb 2018  路  2Comments  路  Source: lovell/sharp

Linux Ubuntu 16.04
Chrome version 64.0.3282.140 (Official Build) (64-bit)

This is what I get when I try to resize a file that doesn't exist.
The issue is the DeprecationWarning below.

const sharp = require ("sharp")

sharp ('/some/dir/notexist.jpg')
    .resize (200)
    .toBuffer ()
    .then (data => {
        console.log ('do something with data')
    })
    .catch ((err) => {
        throw err
    })
(node:21825) UnhandledPromiseRejectionWarning: Error: Input file is missing or of an unsupported image format
(node:21825) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:21825) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Any thoughts?

question

Most helpful comment

Thanks Lovell for the quick response and thanks for this great module.
I thought I'd post a suggestion here in case someone else stumble on the same issue.

const fs = require ("fs")

async function resize (src, dst)
{
    const sharp = require ("sharp")

    await new Promise ((resolve, reject) => {
        sharp (src)
            .resize (200)
            .toBuffer ()
            .then (data => {
                fs.writeFileSync (dst, data)
            })
            .catch ((err) => {
                // do not throw here!
                reject (err)
            })
    })
}

async function main ()
{
    try
    {
        await resize ('/foo/bar.jpg', '/foo/bar-resized.jpg')
    }
    catch (err)
    {
        // handle error
        console.log (err)
    }
}

main ()

All 2 comments

Hello, this is because you're throwing an error in the catch handler of the Promise chain.

StackOverflow may be a better place to ask this more general kind of JavaScript question, e.g. https://stackoverflow.com/questions/30715367/why-can-i-not-throw-inside-a-promise-catch-handler

Thanks Lovell for the quick response and thanks for this great module.
I thought I'd post a suggestion here in case someone else stumble on the same issue.

const fs = require ("fs")

async function resize (src, dst)
{
    const sharp = require ("sharp")

    await new Promise ((resolve, reject) => {
        sharp (src)
            .resize (200)
            .toBuffer ()
            .then (data => {
                fs.writeFileSync (dst, data)
            })
            .catch ((err) => {
                // do not throw here!
                reject (err)
            })
    })
}

async function main ()
{
    try
    {
        await resize ('/foo/bar.jpg', '/foo/bar-resized.jpg')
    }
    catch (err)
    {
        // handle error
        console.log (err)
    }
}

main ()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

paulieo10 picture paulieo10  路  3Comments

henbenla picture henbenla  路  3Comments

knoxcard picture knoxcard  路  3Comments

iq-dot picture iq-dot  路  3Comments

natural-law picture natural-law  路  3Comments