Skip to content

Repositories getters refactoring #55

@kostysh

Description

@kostysh

All repository getters are implemented in the following way:

public async getModifier(key: ModifiersKey): Promise<ModifiersValues> {
    try {
      return await this.db.get(key);
    } catch (e) {
      if (e.status === 404) {
        throw ApiError.NotFound(`Unable to get "${key}" of modifier level"`);
      }
      throw e;
    }
  }

That is mean a getter throws an error in case this modifier is not found.
As it turned out such behaviour complicates getters usage code. When we need to use a modifier as a condition we are forced to use try - catch construct twice like here:

try {
  modifier =
    (await modifiersRepository.getModifier(
      'occupancy'
    )) as OccupancyRateModifier;
} catch (e) {
  if (e.status !== 404) {
    throw e;
  }

  try {
    modifier =
      (await modifierRepository.getModifier(
        'occupancy'
      )) as OccupancyRateModifier;
  } catch (e) {
    if (e.status !== 404) {
      throw e;
    }
  }
}      

I propose to rewrite the getters implementation to not throw errors. If the getter cannot get the value then just return null. This way using getter in conditions will be much simpler.

Metadata

Metadata

Assignees

Labels

C-databaseComponents: databaseD-easyDifficulty: easyP-normalPriority: normalT-debtType: code debt

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions