Added conditional logic to error from writeFileSync