sam-goodwin / itty-aws

A teeny tiny ~49KB AWS SDK for TypeScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proxy for Error Classes

sam-goodwin opened this issue · comments

commented

Can we generate some types for the various AWS SDK errors, e.g. ConditionCheckFailed and then provide a proxy that lazily instantiates a class when accessed?

const dynamo = new AWS.DynamoDB();

try {
  await dynamo.putItem({ .. })
} catch (err) {
  if (err instanceof AWS.DynamoDB.ConditionCheckFailed) {
    
  }
}
commented

Proxy could work like:

let errorClasses = {};

new Proxy({}, {
  get: (_, className) => errorClasses ??= (class extends Error {
    constructor(type: string, message: string) { .. }
  })
}

Hard parts:

  1. how do we enumerate all the different errors?
  2. how do we map AWS's internal error IDs, e.g. __type: 'com.amazon.coral.validate#ValidationException', to a class?