2

I am uploading and deploying sharepoint packages via gulp. Sometimes, it's possible that the app catalog folder doesn't exist, and so the upload will fail. I'd like to just make a note of the error and move on. I've tried to add some error handling to my code but i can't seem to catch it. I've tried inside the pipe, outside, and also a try / catch just after the for loop.

Error

Uploading packge to widget catalog...
[14:29:40] Uploading test.sppkg
[14:29:40] INFO: Folder 'AppCatalog' doesn't exist and will be created
[14:29:40] ERROR: StatusCodeError: 403 - {"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied."}}}

Gulp Code

const gulp = require('gulp');
const build = require('@microsoft/sp-build-web');
const spsync = require('gulp-spsync-creds').sync;
const sppkgDeploy = require('node-sppkg-deploy');


for (const site of sites) {
   console.log("Uploading packge to " + site + " catalog...");
       await new Promise((resolve, reject) => {
          gulp
          .src(folderLocation)
          .pipe(
            spsync({
              username: uname,
              password: pwd,
              site: siteUrl + "/" + site,
              libraryPath: catalogName,
              publish: true,
            })
          )
          .on("finish",() => {
            console.log("Completed upload of:" + name + ". Starting deploy... ");
              sppkgDeploy.deploy({
                username: uname,
                password: pwd,
                absoluteUrl: siteUrl + "/" + site,
                filename: name,
                skipFeatureDeployment: true,
                verbose: false
            });
            resolve();
          })
          .on("error", (e)=>{
            console.log("%%%%%%%%%%FAILED!")
          });
        });
      }

Ultimately, I'd like to catch error number 403 ... and just do a simple console.log and move on. Can someone tell me where I've gone wrong? I'm presently researching gulp plumber(?) in case that's relevant. But this is my first gulp script so .. just checking with the community in case I've gone astray somewhere.

thanks.

EDIT 1

Updated my code so it looks like this:

      await new Promise((resolve, reject) => {
          gulp
          .src(folderLocation)
          .pipe(
            spsync({
              username: uname,
              password: pwd,
              site: siteCatalogUrl + "/",
              libraryPath: catalogName,
              publish: true,
            }).on("error", e => console.log("==@@@@@@@@THIS HAS BOMBED@@@@@@@@@@@@@@")))
          .on("finish",resolve); 
          });

Basically just added error handler for the spsync. But the code never hits there either.

dot
  • 14,928
  • 41
  • 110
  • 218

1 Answers1

0

You are not the first one to have run into this problem. Here is a stackoverflow post, and here is another gist post with basically the same problem described. There seem to be two different possibilities. One is to use gulp-plumber, and the other is to use files-exist. Here is an example of how you can use files-exists:

try {
    return src(filesExist('foo.js'))
        .pipe(src('foo.js'))
        // continue pipe
} catch (error) {
    // error handling
}

I also want to highlight something that I discovered when I experimented with this. If you add the error listener immediately after the src stream is created, then the error is caught, so this will work:

return src('non-existing.js')
    .on('error', () => { console.log('Caught error') })
    ... more pipe stuff

However, if the error listener is added after a pipe call, then it will not work, like so:

return src('non-existing.js')
    .pipe(/* some pipe action */)
    .on('error', () => { console.log('Will not be caught') })
    ... more pipe stuff
joachimwedin
  • 1,544
  • 9
  • 13
  • but the issue i'm dealing isn't the local file not existing ... it's the AppCatalog site not being created in sharepoint... So I think the use case you're describing is very different, no? – dot Aug 25 '21 at 17:29