estruyf / gulp-spsync-creds

Gulp plugin for synchronizing local files with a SharePoint library via user credentials
MIT License
12 stars 4 forks source link

how to capture errors in spsync? #15

Open closetcodebrews opened 3 years ago

closetcodebrews commented 3 years ago

I would like to request help in improving this code so it catches specific errors.
I've deliberately passed an incorrect username to the spsync method... and the gulp script blows up with an appropriate Authentication error that says the user account doesn't exist. But I'd like to catch this specific error, and have the code move on to the next package that I'm trying to upload. (this method is called within a loop...)

I've tried .on("error") in a few spots. but nothing is working.

Here's my code:

function upload(packageName, sites){
    return new Promise((resolve, reject) => {
        const folderLocation = `./plugins/${packageName}`;
        return gulp.src(folderLocation)
            .on('end', function() {console.log('Gulp finished reading source package file')})
            .pipe(
              spsync({
                username: uname,
                password: pwd,
                site: siteCatalogUrl + "/",
                libraryPath: catalogName,
                publish: true,
                verbose: true
              }).on('error', function() {console.log("SPSYNC: something bad happened but this never triggers")})
            ).on('finish', function() {  
              resolve();
              console.log('Gulp pipe finished')})
             //this never triggers either
              .on('error', function(err) {
                  reject();
                  console.log('Gulp pipe failed')
             })
             ; 
      });
}

function uploadSequential() {
  const pluginList = require("./config/pluginlist.json");
  if (!pluginList) {
    return;
  }
  for (const { name, sites } of pluginList.plugins) {
      upload(name, sites)
      .then(result =>console.log("back in the main line"))
      .catch(result => console.log("something failed in upload"));
  }
}

Output

lab3:spplugins admin$ gulp uploadSequential [09:46:14] Using gulpfile /src/spplugins/gulpfile.js [09:46:14] Starting 'uploadSequential'... Upload Invoked for :test.sppkg [09:46:14] Finished 'uploadSequential' after 6.41 ms [09:46:14] Uploading test.sppkg Gulp finished reading source package file Unhandled rejection Error: S:Sender</S:Value> wst:FailedAuthentication</S:Value> </S:Subcode> </S:Code> Authentication Failure</S:Text>

Version information:

lab3:spplugins admin$ gulp -v CLI version: 2.3.0 Local version: 3.9.1

When I upgrade to 4.0.2, this is the error I see:

lab3:spplugins admin$ gulp uploadSequential

[10:07:20] Using gulpfile /src/spplugins/gulpfile.js [10:07:20] Starting 'uploadSequential'... Upload Invoked for :test.sppkg [10:07:20] Uploading test.sppkg Gulp finished reading source package file > (node:26454) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead. Unhandled rejection Error: S:Sender</S:Value> wst:FailedAuthentication</S:Value> </S:Subcode> </S:Code> Authentication Failure</S:Text>

I'm just new to JS Promises and also gulp so any help would be appreciated.

closetcodebrews commented 3 years ago

While waiting for some suggestions, I reviewed the gulp-spsync-creds test.js file ... to see how you are testing your own code.

  1. It doesn't look like I'm doing anything too egregious / different from what you are.
  2. I didn't see any examples of .catch or .on('error') for the spsync call itself. I do some on the API calls you're making.
estruyf commented 3 years ago

Hi @closetcodebrews, it has been a while since I've used this gulp extension. Try to turn on the verbose logging option to get more information about what the plugin tries to do. Also check the special characters in the password. Some of them could cause issues when not escaped properly.

closetcodebrews commented 3 years ago

Hi @closetcodebrews, it has been a while since I've used this gulp extension. Try to turn on the verbose logging option to get more information about what the plugin tries to do. Also check the special characters in the password. Some of them could cause issues when not escaped properly.

howdy! no worries. I understand what it's like jumping between projects. Verbose logging is on. but it's not an issue with a typo. I am purposely trying to catch the error when an incorrect account is used. Apparently, the end users want the code to handle this scenario gracefully because it happens often enough. Hope this clarifies the use case I'm trying to build. Maybe if you could just show me how you would catch errors in general for the code, that might be helpful?