Skip to content

Attachment API isn't working as expected #5080

Closed
@yousefa00

Description

@yousefa00

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/node

SDK Version

6.19.6

Framework Version

No response

Link to Sentry event

No response

Steps to Reproduce

Any idea why this might not be working for me? I have the following Sentry configuration:

const attachmentUrlFromDsn = (dsn: DsnComponents, eventId: string) => {
  const { host, path, projectId, port, protocol, user } = dsn
  return `${protocol}://${host}${port !== '' ? `:${port}` : ''}${
    path !== '' ? `/${path}` : ''
  }/api/${projectId}/events/${eventId}/attachments/?sentry_key=${user}&sentry_version=7&sentry_client=custom-javascript`
}

export const attachAsJson = async (
  attachments: string[],
  event: Sentry.Event,
  client: any
) => {
  const dsn = client?.getDsn()

  if (!dsn || !event.event_id) {
    return
  }

  const endpoint = attachmentUrlFromDsn(dsn, event.event_id)
  logger.info(endpoint)

  const formData = new FormData()
  for (const attachment of attachments) {
    formData.append('attachments', attachment)
  }
  logger.info(formData)

  try {
    await axios.request({
      method: 'POST',
      url: endpoint,
      data: formData,
      headers: {
        'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`
      }
    })
    return event
  } catch (err) {
    logger.error(err)
    return null
  }
}

And include this where the attachment is in scope:

Sentry.addGlobalEventProcessor(async event => {
    try {
      const client = Sentry.getCurrentHub().getClient()
      if (!client) {
        return null
      }
      await attachAsJson([attachment], event, client)
    } catch (ex) {
      logger.error(ex)
    }
    return event
  })

Expected Result

An event is created with the relevant attachment.

Actual Result

I see the Sentry events created correctly, and logging the attachment API request status code is 201 created, yet there are no attachments on the actual event created. Any help would be greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions