<commit_before>'use strict'

const env = require('@codeshare/env')
env.assert([
  'NODE_ENV',
  'APP_NAME',
  'APP_VERSION'
])

const bunyan = require('bunyan')
const client = require('@google/cloud-errors').start({
  reportUncaughtExceptions: false,
  serviceContext: {
    service: process.env.APP_NAME,
    version: process.env.APP_VERSION
  }
})
const errToJSON = require('error-to-json')
const kp = require('keypather')()
const ReqContainer = require('@google/cloud-errors/lib/classes/request-information-container.js')
const through2 = require('through2')

const levelToName = {
  60: 'fatal',
  50: 'error',
  40: 'warn',
  30: 'info',
  20: 'debug',
  10: 'trace'
}

module.exports = {
  type: 'raw',
  level: bunyan.ERROR,
  stream: through2.obj(function (logObj, enc, cb) {
    if (logObj.err && !kp.get(logObj, 'err.data.dontReport')) {
      const ctx = logObj.ctx || {}
      const userId = kp.get(ctx, 'me.id')
      let reqContainer
      if (ctx.req) {
        reqContainer = new RequestContainer()
        reqContainer.setMethod(ctx.req.method)
        reqContainer.setUrl(ctx.req.url)
        reqContainer.setUserAgent(ctx.req.headers['user-agent'])
        reqContainer.setReferrer(ctx.req.headers['referrer'] || ctx.req.headers['referer'])
        if (ctx.req.socket && ctx.req.socket.remoteAddress) {
          reqContainer.setRemoteAddress(ctx.req.socket.remoteAddress)
        }
      } else if (ctx.spark && ctx.rootSpanData) {
        reqContainer = new RequestContainer()
        reqContainer.setMethod('GET')
        reqContainer.setUrl(kp.get(ctx.rootSpanData, 'spanData.name'))
        reqContainer.setRemoteAddress(ctx.spark.headers['x-forwarded-for'] || ctx.spark.remote)
      }
      if (reqContainer) {
        reqContainer.setStatusCode(logObj.err.statusCode || 500)
      }
      logObj.err.stack += '---\nlog.error: ' + logObj.msg
      console.log('REPORT', logObj.err, reqContainer, null, userId, !!cb)
      // client.report(logObj.err, reqContainer, logObj.msg, userId, cb)
      cb()
    } else {
      cb()
    }
  })
}
<commit_msg>fix gcloud errors<commit_after>'use strict'

const env = require('@codeshare/env')
env.assert([
  'NODE_ENV',
  'APP_NAME',
  'APP_VERSION'
])

const bunyan = require('bunyan')
const client = require('@google/cloud-errors').start({
  reportUncaughtExceptions: false,
  serviceContext: {
    service: process.env.APP_NAME,
    version: process.env.APP_VERSION
  }
})
const errToJSON = require('error-to-json')
const kp = require('keypather')()
const ReqContainer = require('@google/cloud-errors/lib/classes/request-information-container.js')
const through2 = require('through2')

const levelToName = {
  60: 'fatal',
  50: 'error',
  40: 'warn',
  30: 'info',
  20: 'debug',
  10: 'trace'
}

module.exports = {
  type: 'raw',
  level: bunyan.ERROR,
  stream: through2.obj(function (logObj, enc, cb) {
    if (logObj.err && !kp.get(logObj, 'err.data.dontReport')) {
      const ctx = logObj.ctx || {}
      const userId = kp.get(ctx, 'me.id')
      let reqContainer
      if (ctx.req) {
        reqContainer = new RequestContainer()
        reqContainer.setMethod(ctx.req.method)
        reqContainer.setUrl(ctx.req.url)
        reqContainer.setUserAgent(ctx.req.headers['user-agent'])
        reqContainer.setReferrer(ctx.req.headers['referrer'] || ctx.req.headers['referer'])
        if (ctx.req.socket && ctx.req.socket.remoteAddress) {
          reqContainer.setRemoteAddress(ctx.req.socket.remoteAddress)
        }
      } else if (ctx.spark && ctx.rootSpanData) {
        reqContainer = new RequestContainer()
        reqContainer.setMethod('GET')
        reqContainer.setUrl(kp.get(ctx.rootSpanData, 'spanData.name'))
        reqContainer.setRemoteAddress(ctx.spark.headers['x-forwarded-for'] || ctx.spark.remote)
      }
      if (reqContainer) {
        reqContainer.setStatusCode(logObj.err.statusCode || 500)
      }
      logObj.err.stack += '---\nlog.error: ' + logObj.msg
      client.report(logObj.err, reqContainer, null, userId, cb)
    } else {
      cb()
    }
  })
}

{'fraction_non_alphanumeric': 0.15517241379310345, 'fraction_numerical': 0.008866995073891626, 'mean_word_length': 3.230208333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2840703', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1364, 'n_words': 231}