Netlify lambda function errors

I have a very simple lambda function that creates a file in a Github repo. Running it locally using netlify-lambda the function completes, successfully writing he file to the github repo, but there are warnings that cause the function to exit in an errored state.

When I run the code outside of the lambda function there are no errors or warnings.

Where are the errors coming from?

can you post the error messages in full, please?

@perry Thanks for the reply. I had originally tried to add the error
messages and some code snippets to the post, but the discourse forum
software kept blocking me from posting. Perhaps I’ll be able to add
the details here by replying to your comment via email reply.

These were the errors:

(node:2612) [DEP0005] DeprecationWarning: Buffer() is deprecated due
to security and usability issues. Please use the Buffer.alloc(),
Buffer.allocUnsafe(), or Buffer.from() methods instead.

(node:2612) UnhandledPromiseRejectionWarning: TypeError: Cannot read
property ‘statusCode’ of undefined

(node:2612) 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:2612) [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.

This is the function:

const github = require(‘octonode’);
const client = github.client(‘REDACTED TOKEN’);

exports.handler = async (event, context, callback) => {
const subject = event.queryStringParameters.name || ‘World’;
console.log(‘making call to github…’);
const ghrepo = client.repo(‘REDACTEDUSERNAME/REPONAME’);
ghrepo.createContents(’_data/test-lambda.json’, ‘commit message’,
‘content’, function(err, status, body, headers) {
if (err) {
return callback(err);
}
console.log(body);
return callback({
statusCode: 200,
body: Hello ${subject}!
});
});
};

Running the same code as a script there are no errors:

#!/usr/bin/env node
const github = require(‘octonode’);
const client = github.client(‘REDACTED TOKEN’);
const ghrepo = client.repo(‘REDACTEDUSERNAME/REPONAME’);
ghrepo.createContents(’_data/test-script.json’, ‘commit message’,
‘content’, function(err, status, body, headers) {
if (err) {
throw(err);
}
console.log(body);
process.exit();
});

Hi, it looks like you’re trying to use the async/await method but also use a (synchronous) callback function. You won’t be able to do that. Use one or the other. Your function is formatted mostly synchronously, so maybe try without the async keyword:

const github = require(‘octonode’);
const client = github.client(‘REDACTED TOKEN’);

exports.handler = (event, context, callback) => {
    const subject = event.queryStringParameters.name || ‘World’;
    console.log(‘making call to github…’);
    const ghrepo = client.repo(‘REDACTEDUSERNAME / REPONAME’);
    ghrepo.createContents(’_data / test - lambda.json’, ‘commit message’, ‘content’, function(err, status, body, headers) {
        if (err) {
            return callback(err);
        }
        console.log(body);
        callback({
            statusCode: 200,
            body: `Hello ${subject}!`
        });
    });
};

@Dennis Thanks for the tip, I ended up solving it with a Promise but your comment helped me see the issue:

const github = require('octonode');
const client = github.client('REDACTED TOKEN');

exports.handler = async event => {
  const subject = event.queryStringParameters.name || 'World';
  console.log('making call to github...');
  const ghrepo = client.repo('REDACTEDUSERNAME/REPONAME');
  return new Promise(function (resolve , reject) {
    ghrepo.createContents('_data/test-lambda11.json', 'commit message', 'content', function(err, status, body, headers) {
      if (err) {
        return reject(err);
      }
      console.log(body);
      return resolve({
        statusCode: 200,
        body: `Hello ${subject}!` 
      });
    });
  });
};

I still can’t figure out how to paste code snippets into the forum. OK I figured the code paste out finally.

FYI, I used 3 backticks, which is exactly what I did initially but was blocked by the discourse forum software from posting unless I removed all code and error messages. I just tried to update the previous posts that have the horrible looking code snippets, but the edit button no longer appears on the posts.

1 Like

awesome! Yea, promises and async/await can be a bit tricky. Glad you got things working!