-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chapter 05: copy logAnalyzer.js, errors and fakes from the folder of …
…chapter 04 in order to continue with the book
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class ArgumentError extends Error {} | ||
|
||
module.exports = ArgumentError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function fakeWebService() { | ||
let lastError; | ||
|
||
/** | ||
* @param {string} message | ||
*/ | ||
function logError(message) { | ||
lastError = message; | ||
} | ||
|
||
function getLastError() { | ||
return lastError; | ||
} | ||
|
||
return { | ||
logError, | ||
getLastError, | ||
}; | ||
} | ||
|
||
module.exports = fakeWebService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const ArgumentError = require('./errors/ArgumentError'); | ||
|
||
function logAnalyzer(extensionManager, webService) { | ||
/** | ||
* @type {boolean} | ||
*/ | ||
let wasLastFileNameValid; | ||
|
||
/** | ||
* @return {boolean} | ||
*/ | ||
function getWasLastFileNameValid() { | ||
return wasLastFileNameValid; | ||
} | ||
|
||
/** | ||
* @param {string} fileName | ||
* @return {Promise} | ||
*/ | ||
async function isValidLogFileName(fileName) { | ||
wasLastFileNameValid = false; | ||
|
||
if (fileName === '') { | ||
throw new ArgumentError('filename has to be provided'); | ||
} | ||
|
||
const result = await extensionManager.isValid(fileName); | ||
|
||
if (!result) { | ||
return false; | ||
} | ||
|
||
wasLastFileNameValid = true; | ||
return true; | ||
} | ||
|
||
/** | ||
* @param {string} fileName | ||
*/ | ||
function analyze(fileName) { | ||
if (fileName.length < 8) { | ||
webService.logError(`Filename too short: ${fileName}`); | ||
} | ||
} | ||
|
||
return { | ||
getWasLastFileNameValid, | ||
isValidLogFileName, | ||
analyze, | ||
}; | ||
} | ||
|
||
module.exports = logAnalyzer; |