Skip to content

Commit

Permalink
Merge pull request #2332 from alphagov/error-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Sep 18, 2023
2 parents bebeed1 + 924d6cb commit 40af747
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/sync-changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const EventEmitter = require('events')

// npm dependencies
const browserSync = require('browser-sync')
const { writeJsonSync } = require('fs-extra')
const { ensureDirSync, writeJsonSync } = require('fs-extra')
const path = require('path')
const util = require('util')
const { tmpDir } = require('./utils/paths')
const fs = require('fs')

Expand All @@ -19,7 +20,15 @@ function hasRestartedAfterError () {
}

function flagError (error) {
writeJsonSync(path.join(tmpDir, 'errors.json'), { error })
const errorFormatted = util.inspect(error, {
compact: false,
depth: Infinity,
maxArrayLength: Infinity,
maxStringLength: Infinity
})

ensureDirSync(path.dirname(errorsFile))
writeJsonSync(errorsFile, { error: errorFormatted })
}

function unflagError () {
Expand Down
3 changes: 2 additions & 1 deletion lib/sync-changes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ describe('sync-changes', () => {

it('flags error correctly', () => {
const error = { data: true }
const errorFormatted = '{\n data: true\n}'

syncChanges.flagError(error)

expect(fse.writeJsonSync).toHaveBeenCalledTimes(1)
expect(fse.writeJsonSync).toHaveBeenCalledWith(errorsFile, { error })
expect(fse.writeJsonSync).toHaveBeenCalledWith(errorsFile, { error: errorFormatted })
})

it('syncs correctly', () => {
Expand Down

0 comments on commit 40af747

Please sign in to comment.