From f0cd06fbddd3c945976408c5afce5c3507ac70a1 Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 29 Jun 2018 11:13:16 +0300 Subject: [PATCH] Check for file existence instead of exception handling #33 --- server/src/services/segments.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/server/src/services/segments.ts b/server/src/services/segments.ts index 61aefb3..604808d 100644 --- a/server/src/services/segments.ts +++ b/server/src/services/segments.ts @@ -1,4 +1,5 @@ import * as path from 'path'; +import * as fs from 'fs'; import { getJsonDataSync, writeJsonDataSync } from './json'; import { SEGMENTS_PATH } from '../config'; import { AnomalyId, loadAnomalyById, saveAnomaly } from './anomalyType'; @@ -8,18 +9,17 @@ import * as _ from 'lodash'; function getLabeledSegments(anomalyId: AnomalyId) { let filename = path.join(SEGMENTS_PATH, `${anomalyId}_labeled.json`); - let segments = []; - try { - segments = getJsonDataSync(filename); - for (let segment of segments) { - if (segment.labeled === undefined) { + if(!fs.existsSync(filename)) { + return []; + } else { + let segments = getJsonDataSync(filename); + for(let segment of segments) { + if(segment.labeled === undefined) { segment.labeled = false; } } - } catch(e) { - segments = []; + return segments; } - return segments; } function getPredictedSegments(anomalyId: AnomalyId) {