storacha / w3cli

💾 w3 command line interface
Other
35 stars 7 forks source link

feat: w3 usage report catches/warns about errors invoking usage/report #169

Closed gobengo closed 9 months ago

gobengo commented 9 months ago

Motivation:

gobengo commented 9 months ago

When it encounters these errors, it still shows them on stderr (rationale: they're ugly, but rare, so showing them shouldn't affect anyone that isn't already getting a full blowup, but hiding the stack traces would make debugging them a bit harder)

Sample runs in my weird state

redirect stderr to /dev/null and see output like before.

w3cli on  issues/168 [$] is 📦 6.0.0 via ⬢ v20.11.0 took 31.7s 
âš¡ 2>/dev/null w3 usage report 
 Account: did:mailto:dag.house:bengo
Provider: did:web:nft.storage
   Space: did:key:z6MktxMvmk3ii638jWcuqzUP3o2A4x2cFnqh9PVKzsaofN1c
    Size: 0

 Account: did:mailto:dag.house:bengo
Provider: did:web:nft.storage
   Space: did:key:z6Mkp5SqdMbRvDQneB4YvwkodATwuhdQAwoFNU9vsJMbetpq
    Size: 0

 Account: did:mailto:dag.house:bengo
Provider: did:web:web3.storage
   Space: did:key:z6Mkr66sQHvPgzMtF7t6GCFr1nk5mZR9F2xZrvb38pNaUycY
    Size: 0

^C

stdout is still JSON so can be piped. But errors will still show on stderr.

âš¡ w3 usage report --json | jq .
error getting usage report. Catching and moving on, but will include in warning at end. {
  error: Error: failed usage/report invocation
      at UsageClient.report (file:///Users/bengo/web3-storage/w3cli/node_modules/@web3-storage/w3up-client/src/capability/usage.js:36:13)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async getSpaceUsageReports (file:///Users/bengo/web3-storage/w3cli/index.js:586:20)
      at async usageReport (file:///Users/bengo/web3-storage/w3cli/index.js:524:20) {
    [cause]: {
      name: 'Unauthorized',
      stack: 'Unauthorized: Claim {"can":"usage/report"} is not authorized\n' +
        '  - Capability {"can":"usage/report","with":"did:key:z6MkqCMroF8KPCVgEvTBcvKgq4fopxdhAfr96aJzwp9Wsg2r","nb":{"period":{"from":1701388800,"to":1706119893}}} is not authorized because:\n' +
        "    - Capability can not be (self) issued by 'did:key:z6MkfbsERaJ7rtJQWMWtYMxNED56bhQMgrNu8CRdUjB5LfRp'\n" +
        '    - Delegated capability not found\n' +
        '    at claim (/node_modules/@ucanto/validator/src/lib.js:308:12)\n' +
        '    at processTicksAndRejections (node:internal/process/task_queues:96:5)\n' +
        '    at Object.report (/node_modules/@ucanto/server/src/handler.js:61:27)\n' +
        '    at invoke2 (/node_modules/@ucanto/server/src/server.js:145:23)\n' +
        '    at async Promise.all (index 0)\n' +
        '    at execute (/node_modules/@ucanto/server/src/server.js:104:5)\n' +
        '    at ucanInvocationRouter (/upload-api/functions/ucan-invocation-router.js:260:20)\n' +
        '    at Runtime.handler (/node_modules/src/awslambda.ts:332:1)',
      message: 'Claim {"can":"usage/report"} is not authorized\n' +
        '  - Capability {"can":"usage/report","with":"did:key:z6MkqCMroF8KPCVgEvTBcvKgq4fopxdhAfr96aJzwp9Wsg2r","nb":{"period":{"from":1701388800,"to":1706119893}}} is not authorized because:\n' +
        "    - Capability can not be (self) issued by 'did:key:z6MkfbsERaJ7rtJQWMWtYMxNED56bhQMgrNu8CRdUjB5LfRp'\n" +
        '    - Delegated capability not found'
    }
  },
  space: 'did:key:z6MkqCMroF8KPCVgEvTBcvKgq4fopxdhAfr96aJzwp9Wsg2r',
  period: { from: 2023-12-01T00:00:00.000Z, to: 2024-01-24T18:11:33.553Z },
  consumers: [ 'did:key:z6MkqCMroF8KPCVgEvTBcvKgq4fopxdhAfr96aJzwp9Wsg2r' ],
  account: 'did:mailto:dag.house:bengo'
}
{
  "account": "did:mailto:dag.house:bengo",
  "provider": "did:web:nft.storage",
  "reportedAt": "2024-01-24T18:11:33.553Z",
  "size": {
    "final": 0,
    "initial": 0
  },
  "space": "did:key:z6MktxMvmk3ii638jWcuqzUP3o2A4x2cFnqh9PVKzsaofN1c"
}

non json output warns about errors at the end

   # ...
   Total: 40941887280

   WARNING: there were 30 errors getting usage reports for some spaces.
   This may happen if your agent does not have usage/report authorization for a space.
   These spaces were not included in the usage report total:
   * space: did:key:z6MkkzxLtWpqXQPPXLEjD2M2gWJZikcpCbdk4HBf7tj7K59j
     error: failed usage/report invocation
     account: did:mailto:dag.house:bengo
   * space: did:key:z6Mkvbww5VWYaeGqASXPkz7hAvFk1igodxiyrKTctk1UvNyE
     error: failed usage/report invocation
     account: did:mailto:dag.house:bengo
   * space: did:key:z6Mked63DNosi2NX6HdQ8xmos2S4D8cT9oJuBRu4gymFrVm7
     error: failed usage/report invocation
     account: did:mailto:dag.house:bengo