Skip to content

Commit

Permalink
fix(logging): do not log invalid dates in data to Sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
sleidig committed Jan 2, 2025
1 parent a2a4be5 commit f16617e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/app/core/basic-datatypes/date/date.datatype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe("Schema data type: date", () => {
expect(actualAnonymized).toEqual(new Date(2023, 6, 1));
});

it("should log warning if transformation fails", () => {
spyOn(Logging, "warn");
it("should log (debug) if transformation fails", () => {
spyOn(Logging, "debug");
const datatype = new DateDatatype();

const result = datatype.transformToObjectFormat("invalidDate", null, {
Expand All @@ -27,6 +27,6 @@ describe("Schema data type: date", () => {
});

expect(result).toBeUndefined();
expect(Logging.warn).toHaveBeenCalled();
expect(Logging.debug).toHaveBeenCalled();
});
});
4 changes: 2 additions & 2 deletions src/app/core/basic-datatypes/date/date.datatype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { Logging } from "../../logging/logging.service";
* `@DatabaseField() myDate: Date; // will be a valid Date even if the database previously had "2020-01-15" as string`
*/
@Injectable()
export class DateDatatype<DBFormat = any> extends DefaultDatatype<
export class DateDatatype<DBFormat = string> extends DefaultDatatype<
Date,
DBFormat
> {
Expand All @@ -54,7 +54,7 @@ export class DateDatatype<DBFormat = any> extends DefaultDatatype<
) {
const date = new Date(value);
if (Number.isNaN(date.getTime())) {
Logging.warn(
Logging.debug(
`failed to convert data '${value}' to Date object for ${parent?._id}`,
);
return undefined;
Expand Down
15 changes: 5 additions & 10 deletions src/app/core/basic-datatypes/month/month.datatype.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable } from "@angular/core";
import { DateOnlyDatatype } from "../date-only/date-only.datatype";
import { DateDatatype } from "../date/date.datatype";
import { EntitySchemaField } from "../../entity/schema/entity-schema-field";
import { Logging } from "../../logging/logging.service";

/**
* Datatype for the EntitySchemaService transforming Date values to/from a short string month format ("YYYY-mm").
Expand All @@ -13,7 +12,7 @@ import { Logging } from "../../logging/logging.service";
* `@DatabaseField({dataType: 'month'}) myMonth: Date = new Date('2020-01-15'); // will be "2020-01" in the database`
*/
@Injectable()
export class MonthDatatype extends DateOnlyDatatype {
export class MonthDatatype extends DateDatatype {
static override dataType = "month";
static override label: string = $localize`:datatype-label:month (date without day of month)`;

Expand All @@ -38,12 +37,8 @@ export class MonthDatatype extends DateOnlyDatatype {
) {
const values = value.split("-").map((v) => Number(v));
const date = new Date(values[0], values[1] - 1);
if (Number.isNaN(date.getTime())) {
Logging.warn(
`failed to convert data '${value}' to Date object for ${parent?._id}`,
);
return undefined;
}
return date;

// re-use error logging and basic return logic from base type
return super.transformToObjectFormat(date, schemaField, parent);
}
}

0 comments on commit f16617e

Please sign in to comment.