try{constresponse=awaitthis.helpers.requestWithAuthentication.call(this,credentialType,options);returnresponse;}catch(error){if(error.httpCode==="404"){constresource=this.getNodeParameter("resource",0)asstring;consterrorOptions={message:`${resource.charAt(0).toUpperCase()+resource.slice(1)} not found`,description:"The requested resource could not be found. Please check your input parameters.",};thrownewNodeApiError(this.getNode(),errorasJsonObject,errorOptions);}if(error.httpCode==="401"){thrownewNodeApiError(this.getNode(),errorasJsonObject,{message:"Authentication failed",description:"Please check your credentials and try again.",});}thrownewNodeApiError(this.getNode(),errorasJsonObject);}
constemail=this.getNodeParameter("email",itemIndex)asstring;if(email.indexOf("@")===-1){constdescription=`The email address '${email}' in the 'email' field isn't valid`;thrownewNodeOperationError(this.getNode(),"Invalid email address",{description,itemIndex,// for multiple items, this will link the error to the specific item});}
处理多个数据项时,包含数据项索引以获得更好的错误上下文:
1 2 3 4 5 6 7 8 91011121314151617181920
for(leti=0;i<items.length;i++){try{// Process itemconstresult=awaitprocessItem(items[i]);returnData.push(result);}catch(error){if(this.continueOnFail()){returnData.push({json:{error:error.message},pairedItem:{item:i},});continue;}thrownewNodeOperationError(this.getNode(),errorasError,{description:error.description,itemIndex:i,});}}