@@ -144,7 +144,7 @@ func sendTelegramMessage(_ message: String, to chatID: Int, image: String? = nil
144144}
145145
146146func handleScreenshotError( entry: URLEntry ) throws {
147- let errorFile = " \( directory ( for: entry) ) /error "
147+ let errorFile = " \( directory ( for: entry) ) /. error "
148148 // If the errorReportMinutes are not set, we immediately notify the user
149149 guard kErrorReportDuration > 0 else {
150150 try notifyError ( entry: entry)
@@ -158,15 +158,19 @@ func handleScreenshotError(entry: URLEntry) throws {
158158 if !fileManager. fileExists ( atPath: errorFile) {
159159 print ( " Creating error file at \( errorFile) " )
160160 // If the error file does not exist yet, create a new one and return
161- fileManager. createFile ( atPath: errorFile, contents: nil )
161+ let content = ISO8601DateFormatter ( ) . string ( from: Date ( ) )
162+ try content. write ( toFile: errorFile, atomically: true , encoding: . utf8)
162163 return
163164 }
164165
165- // If an error file already exists, get the attributes and check the creation date
166+ // If an error file already exists, read the creation date from the file's contents
166167 print ( " Error file already exists " )
167- if fileManager. fileExists ( atPath: errorFile) ,
168- let creationDate = try fileManager. attributesOfItem ( atPath: errorFile) [ . creationDate] as? Date {
169- print ( " Creation date: \( creationDate) ( \( creationDate. distance ( to: Date ( ) ) / 3600 ) hours ago " )
168+ if fileManager. fileExists ( atPath: errorFile) {
169+ let errorContents = try String ( contentsOfFile: errorFile)
170+ print ( " Creation date: \( errorContents) " )
171+ guard let creationDate = ISO8601DateFormatter ( ) . date ( from: errorContents) else {
172+ throw JFUrlwatcherError . noErrorCreationDate ( entry)
173+ }
170174 if creationDate. distance ( to: Date ( ) ) >= kErrorReportDuration {
171175 // Notify the user that an error persisted for the last `errorReportTime` seconds
172176 try notifyError ( entry: entry)
@@ -183,8 +187,8 @@ func notifyError(entry: URLEntry) throws {
183187 // TODO: Replace with DateComponentsFormatter when available on Linux
184188 let f = SharedUtils . DummyFormatter ( fullUnits: true )
185189 let durationString = f. string ( from: kErrorReportDuration) ?? " some time "
186- try sendTelegramMessage ( " The entry ' \( entry. name) ' failed to capture a screenshot for at least " +
187- " \( durationString. isEmpty ? " some time " : durationString) . " ,
190+ try sendTelegramMessage ( " The entry ' \( entry. name) ' failed to capture a screenshot for " +
191+ " \( durationString. isEmpty ? " some time " : " at least \( durationString) " ) . " ,
188192 to: Int ( entry. chatID) )
189193}
190194
0 commit comments