Angular 6 SSH2-如何使用承诺处理错误?

时间:2018-10-27 03:30:13

标签: node.js angular electron angular6 ssh2-sftp

我正在尝试使用angular来处理ssh2中的错误消息

我试图做出承诺来处理它,但是它不起作用,我是新来的,抱歉,如果它太糟糕了,但是我不知道如何处理它。

这是我的承诺

const ssh = new Promise(function(resolve, reject) {
  const conn = new ssh2Client.Client();
  conn.on('ready', function() {
    console.log('ready');
    conn.sftp(function(err, sftp) {
      sftp.readdir('/', function(error, list) {
        resolve(list);
        console.dir(list);
        conn.end();
        if (err) {
          reject(err);
        }
      });
    });
  }).connect({
  host: '192.168.4.127',
  port: 22,
  username: 'root',
  password: ''
  });
  });

  ssh.then((success)=>{
    console.log('ok',success);
  }).catch((err)=>{
    console.log('err',err);
  });

这是我的错误消息:

zone-mix.js:3292 ERROR Error: Timed out while waiting for handshake
    at client.js:695
    at ZoneDelegate.push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invokeTask (zone-mix.js:421)
    at Object.onInvokeTask (core.js:3815)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invokeTask (zone-mix.js:420)
    at Zone.push../node_modules/zone.js/dist/zone-mix.js.Zone.runTask (zone-mix.js:188)
    at push../node_modules/zone.js/dist/zone-mix.js.ZoneTask.invokeTask (zone-mix.js:496)
    at ZoneTask.invoke (zone-mix.js:485)
    at timer (zone-mix.js:2074)
console.(anonymous function) @ zone-mix.js:3292
defaultErrorLogger @ core.js:1673
push../node_modules/@angular/core/fesm5/core.js.ErrorHandler.handleError @ core.js:1719
next @ core.js:4319
schedulerFn @ core.js:3555
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub @ Subscriber.js:195
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next @ Subscriber.js:133
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next @ Subscriber.js:77
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next @ Subscriber.js:54
push../node_modules/rxjs/_esm5/internal/Subject.js.Subject.next @ Subject.js:47
push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit @ core.js:3539
(anonymous) @ core.js:3846
push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invoke @ zone-mix.js:388
push../node_modules/zone.js/dist/zone-mix.js.Zone.run @ zone-mix.js:138
push../node_modules/@angular/core/fesm5/core.js.NgZone.runOutsideAngular @ core.js:3783
onHandleError @ core.js:3846
push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.handleError @ zone-mix.js:392
push../node_modules/zone.js/dist/zone-mix.js.Zone.runTask @ zone-mix.js:191
push../node_modules/zone.js/dist/zone-mix.js.ZoneTask.invokeTask @ zone-mix.js:496
ZoneTask.invoke @ zone-mix.js:485
timer @ zone-mix.js:2074
setTimeout (async)
scheduleTask @ zone-mix.js:2095
push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.scheduleTask @ zone-mix.js:407
onScheduleTask @ zone-mix.js:297
push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.scheduleTask @ zone-mix.js:401
push../node_modules/zone.js/dist/zone-mix.js.Zone.scheduleTask @ zone-mix.js:232
push../node_modules/zone.js/dist/zone-mix.js.Zone.scheduleMacroTask @ zone-mix.js:255
scheduleMacroTaskWithCurrentZone @ zone-mix.js:1114
(anonymous) @ zone-mix.js:2110
proto.(anonymous function) @ zone-mix.js:1394
startTimeout @ client.js:694
doConnect @ client.js:660
push../node_modules/ssh2/lib/client.js.Client.connect @ client.js:668
(anonymous) @ heroes.component.ts:46
ZoneAwarePromise @ zone-mix.js:891
push../src/app/components/heroes/heroes.component.ts.HeroesComponent.ngOnInit @ heroes.component.ts:32
checkAndUpdateDirectiveInline @ core.js:9250
checkAndUpdateNodeInline @ core.js:10514
checkAndUpdateNode @ core.js:10476
debugCheckAndUpdateNode @ core.js:11109
debugCheckDirectivesFn @ core.js:11069
(anonymous) @ HeroesComponent_Host.ngfactory.js? [sm]:1
debugUpdateDirectives @ core.js:11061
checkAndUpdateView @ core.js:10458
callViewAction @ core.js:10699
execEmbeddedViewsAction @ core.js:10662
checkAndUpdateView @ core.js:10459
callViewAction @ core.js:10699
execComponentViewsAction @ core.js:10641
checkAndUpdateView @ core.js:10464
callWithDebugContext @ core.js:11351
debugCheckAndUpdateView @ core.js:11029
push../node_modules/@angular/core/fesm5/core.js.ViewRef_.detectChanges @ core.js:8845
(anonymous) @ core.js:4581
push../node_modules/@angular/core/fesm5/core.js.ApplicationRef.tick @ core.js:4581
(anonymous) @ core.js:4473
push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invoke @ zone-mix.js:388
onInvoke @ core.js:3824
push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invoke @ zone-mix.js:387
push../node_modules/zone.js/dist/zone-mix.js.Zone.run @ zone-mix.js:138
push../node_modules/@angular/core/fesm5/core.js.NgZone.run @ core.js:3738
next @ core.js:4473
schedulerFn @ core.js:3555
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub @ Subscriber.js:195
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next @ Subscriber.js:133
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next @ Subscriber.js:77
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next @ Subscriber.js:54
push../node_modules/rxjs/_esm5/internal/Subject.js.Subject.next @ Subject.js:47
push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit @ core.js:3539
checkStable @ core.js:3793
onHasTask @ core.js:3837
push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.hasTask @ zone-mix.js:441
push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate._updateTaskCount @ zone-mix.js:461
push../node_modules/zone.js/dist/zone-mix.js.Zone._updateTaskCount @ zone-mix.js:285
push../node_modules/zone.js/dist/zone-mix.js.Zone.runTask @ zone-mix.js:205
drainMicroTaskQueue @ zone-mix.js:595
Promise resolved (async)
scheduleMicroTask @ zone-mix.js:578
push../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.scheduleTask @ zone-mix.js:410
push../node_modules/zone.js/dist/zone-mix.js.Zone.scheduleTask @ zone-mix.js:232
push../node_modules/zone.js/dist/zone-mix.js.Zone.scheduleMicroTask @ zone-mix.js:252
scheduleResolveOrReject @ zone-mix.js:862
ZoneAwarePromise.then @ zone-mix.js:962
push../node_modules/@angular/core/fesm5/core.js.PlatformRef.bootstrapModule @ core.js:4353
./src/main.ts @ main.ts:12
__webpack_require__ @ bootstrap:76
0 @ polyfills.ts:84
__webpack_require__ @ bootstrap:76
checkDeferredModules @ bootstrap:43
webpackJsonpCallback @ bootstrap:30
(anonymous) @ main.js:1
zone-mix.js:3292 ERROR Error: connect ETIMEDOUT 192.168.4.127:22
    at Object._errnoException (util.js:1024)
    at _exceptionWithHostPort (util.js:1046)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182)

没关系,我知道发生了什么,问题是我想在成功时和发生错误时进行处理,例如,如果将连接设置为console.log即可,如果不是,则console.error为错误。

我基本上需要的是兑现诺言,有人可以帮助我吗?

非常感谢您,如果我不清楚,请对不起。

1 个答案:

答案 0 :(得分:0)

JB Nizet 所述,我已通过这种方式对其进行了修复

const ssh = new Promise(function (resolve, reject) {
  const conn = new ssh2Client.Client();
  conn.on('error', function (error) {
    if (error.level) {
      reject(error);
    }
    conn.end();
  });
  conn.on('ready', function () {
    resolve();
  })
    .connect(connObject);
});
return ssh;

我必须先处理错误才能拒绝,否则要兑现承诺