Skip to content

Commit

Permalink
fix: improve error handling in get data route
Browse files Browse the repository at this point in the history
  • Loading branch information
YutaroNegi committed Dec 1, 2024
1 parent e68718c commit 27cc3b3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('SensorAggregatedService', () => {

it('should throw an error for invalid interval', async () => {
await expect(service.get({ interval: 'invalid' as any })).rejects.toThrow(
'Invalid interval provided. Use "24h", "48h", "1w", or "1m".',
'Invalid interval. Supported values are 24h, 48h, 1w, 1m',
);
});
});
11 changes: 5 additions & 6 deletions src/modules/sensor-aggregated/sensor-aggragated.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Inject } from '@nestjs/common';
import { Injectable, Inject, HttpException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { DynamoDB } from 'aws-sdk';
import { GetSensorAggregatedDto } from './dto/create-aggregated.dto';
Expand Down Expand Up @@ -28,8 +28,9 @@ export class SensorAggregatedService {

const hours = intervalMap[interval];
if (!hours) {
throw new Error(
'Invalid interval provided. Use "24h", "48h", "1w", or "1m".',
throw new HttpException(
'Invalid interval. Supported values are 24h, 48h, 1w, 1m',
400
);
}

Expand Down Expand Up @@ -83,9 +84,7 @@ export class SensorAggregatedService {
items,
};
} catch (error) {
throw new Error(
`Error retrieving aggregated sensor data in DynamoDB: ${error.message}`,
);
throw new HttpException(error.message, 500);
}
}
}

0 comments on commit 27cc3b3

Please sign in to comment.