{"id":11073,"date":"2024-02-29T05:55:37","date_gmt":"2024-02-29T10:55:37","guid":{"rendered":"https:\/\/www.redline13.com\/blog\/?p=11073"},"modified":"2024-02-29T05:55:37","modified_gmt":"2024-02-29T10:55:37","slug":"k6-error-handling","status":"publish","type":"post","link":"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/","title":{"rendered":"k6 Error Handling"},"content":{"rendered":"<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11074\" src=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/02\/word-image-11073-1.png\" alt=\"k6 Error Handling\" width=\"400\" height=\"300\" srcset=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/02\/word-image-11073-1.png 400w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/02\/word-image-11073-1-300x225.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/strong><\/p>\n<p>When testing at scale, it is inevitable that at some point your load test will encounter error states. It may even be a condition itself that is tested. Therefore, it is essential that we have a capability to capture and analyze error information. In this brief article, we will detail how to intercept and log errors in your <a href=\"https:\/\/k6.io\/\" target=\"_blank\" rel=\"noopener\">k6<\/a> load tests.<\/p>\n<h3><a id=\"post-11073-_e9j11itg3tzf\"><\/a>Intercepting and Logging Errors<\/h3>\n<p>Ideally, we would like to save errors to a retrievable log file that can be reviewed after the test completes. In order to do this, we must implement a class that instructs k6 on what to do with error information. Here is the example class provided in the <a href=\"https:\/\/grafana.com\/docs\/k6\/latest\/examples\/error-handler\/\" target=\"_blank\" rel=\"noopener\">official k6 documentation<\/a>:<\/p>\n<pre><strong>class ErrorHandler {<\/strong>\n<strong>  constructor(logErrorDetails) {<\/strong>\n<strong>    this.logErrorDetails = logErrorDetails;<\/strong>\n<strong>  }<\/strong>\n<strong>  logError(isError, res, tags = {}) {<\/strong>\n<strong>    if (!isError) return;<\/strong>\n<strong>    const traceparentHeader = res.request.headers['Traceparent'];<\/strong>\n<strong>    const errorData = Object.assign(<\/strong>\n<strong>      {<\/strong>\n<strong>        url: res.url,<\/strong>\n<strong>        status: res.status,<\/strong>\n<strong>        error_code: res.error_code,<\/strong>\n<strong>        traceparent: traceparentHeader &amp;&amp; traceparentHeader.toString(),<\/strong>\n<strong>      },<\/strong>\n<strong>      tags<\/strong>\n<strong>    );<\/strong>\n<strong>    this.logErrorDetails(errorData);<\/strong>\n<strong>  }<\/strong>\n<strong>}<\/strong><\/pre>\n<p>The next step is to initialize the <code>ErrorHandler<\/code> object in our code, which will make invoking on errors more convenient:<\/p>\n<pre><strong>const errorHandler = new ErrorHandler((error) =&gt; {<\/strong>\n<strong>  console.error(error);<\/strong>\n<strong>});<\/strong><\/pre>\n<p>When implementing error handling, the <a href=\"https:\/\/k6.io\/docs\/using-k6\/checks\/\" target=\"_blank\" rel=\"noopener\">checks<\/a> module that k6 provides is particularly useful. This allows us to validate a boolean condition and take an action. Here is the basic example provided in the k6 documentation:<\/p>\n<pre><strong>import http from 'k6\/http';<\/strong>\n<strong>import { check } from 'k6';<\/strong>\n\n<strong>export default function () {<\/strong>\n<strong>\u00a0 let res, checkStatus;<\/strong>\n<strong>\u00a0 res = http.get('https:\/\/httpbin.org\/status\/200');<\/strong>\n<strong>\u00a0 checkStatus = check(res, { 'status is 200': (res) =&gt; res.status === 200 });<\/strong>\n<strong>\u00a0 errorHandler.logError(!checkStatus, res);<\/strong>\n<strong>\u00a0 res = http.get('https:\/\/httpbin.org\/status\/300');<\/strong>\n<strong>\u00a0 checkStatus = check(res, { 'status is 200': (res) =&gt; res.status === 200 });<\/strong>\n<strong>\u00a0 errorHandler.logError(!checkStatus, res);<\/strong>\n<strong>}<\/strong><\/pre>\n<p>In the above code, <a href=\"https:\/\/httpbin.org\/\" target=\"_blank\" rel=\"noopener\">test endpoints<\/a> are used where the first HTTP request causes a <code>200<\/code> response to be returned, while the second example returns a <code>300<\/code> response. Our check is configured such that an error will be logged whenever either request returns a response code other than <code>200<\/code>.<\/p>\n<h3><a id=\"post-11073-_jde469nryz97\"><\/a>Reviewing Errors<\/h3>\n<p>By default, errors logged in this manner go to the configured output. When running from the command line this is typically the console. When running your test on RedLine13 with <a href=\"https:\/\/www.redline13.com\/blog\/docs\/output-files\/\" target=\"_blank\" rel=\"noopener\">output files<\/a> enabled, errors will appear in the <code>logs.out<\/code> file. Here is an example of how the error from a request above would be logged:<\/p>\n<pre><strong>time=\"21:44:08-05:00\" level=error msg=\"{\\\"url\\\":\\\"https:\/\/httpbin.org\/status\/300\\\",\\\"status\\\":300,\\\"error_code\\\":0}\" source=console<\/strong><\/pre>\n<p>This log entry captures the basic information regarding the unexpected response to our request. By modifying the <code>ErrorHandler<\/code> class, it is possible to capture as much detail as desired.<\/p>\n<hr \/>\n<p>Did you know that RedLine13 offers a full-featured, time-limited free trial? <a href=\"https:\/\/www.redline13.com\/Service\" target=\"_blank\" rel=\"noopener\">Sign up now<\/a>, and start testing with k6 in the cloud today.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When testing at scale, it is inevitable that at some point your load test will encounter error states. It may even be a condition itself that is tested. Therefore, it is essential that we have a capability to capture and analyze error information. In this brief article, we will detail how to intercept and log errors in your k6 load tests. Intercepting and Logging Errors Ideally, we would like to save errors to a retrievable log<a class=\"more-link\" href=\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/\">Read More &rarr;<\/a><\/p>\n","protected":false},"author":11,"featured_media":11074,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[128,195,596,679,671,318,694,424],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-dkoziel","4":"post-11073","6":"format-standard","7":"has-post-thumbnail","8":"category-blog","9":"category-uncategorized","10":"post_tag-cloud-load-testing","11":"post_tag-error-handling","12":"post_tag-k6","13":"post_tag-k6-extensions","14":"post_tag-k6-load-tests","15":"post_tag-load-testing","16":"post_tag-logging","17":"post_tag-redline13"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>k6 Error Handling - RedLine13<\/title>\n<meta name=\"description\" content=\"In this brief article, we will detail how to intercept and log errors in your k6 load tests, as well as retrieve them for later review.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"k6 Error Handling - RedLine13\" \/>\n<meta property=\"og:description\" content=\"In this brief article, we will detail how to intercept and log errors in your k6 load tests, as well as retrieve them for later review.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"RedLine13\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-29T10:55:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/02\/word-image-11073-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"David Koziel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"David Koziel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/\"},\"author\":{\"name\":\"David Koziel\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20\"},\"headline\":\"k6 Error Handling\",\"datePublished\":\"2024-02-29T10:55:37+00:00\",\"dateModified\":\"2024-02-29T10:55:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/\"},\"wordCount\":326,\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"keywords\":[\"Cloud Load Testing\",\"Error Handling\",\"k6\",\"k6 extensions\",\"k6 load tests\",\"Load Testing\",\"logging\",\"RedLine13\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/\",\"name\":\"k6 Error Handling - RedLine13\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\"},\"datePublished\":\"2024-02-29T10:55:37+00:00\",\"dateModified\":\"2024-02-29T10:55:37+00:00\",\"description\":\"In this brief article, we will detail how to intercept and log errors in your k6 load tests, as well as retrieve them for later review.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.redline13.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"k6 Error Handling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"name\":\"RedLine13\",\"description\":\"(Almost) Free Load Testing in the Cloud\",\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.redline13.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\",\"name\":\"RedLine13\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"contentUrl\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"width\":300,\"height\":68,\"caption\":\"RedLine13\"},\"image\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20\",\"name\":\"David Koziel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g\",\"caption\":\"David Koziel\"},\"url\":\"https:\/\/www.redline13.com\/blog\/author\/dkoziel\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"k6 Error Handling - RedLine13","description":"In this brief article, we will detail how to intercept and log errors in your k6 load tests, as well as retrieve them for later review.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/","og_locale":"en_US","og_type":"article","og_title":"k6 Error Handling - RedLine13","og_description":"In this brief article, we will detail how to intercept and log errors in your k6 load tests, as well as retrieve them for later review.","og_url":"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/","og_site_name":"RedLine13","article_published_time":"2024-02-29T10:55:37+00:00","og_image":[{"width":400,"height":300,"url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/02\/word-image-11073-1.png","type":"image\/png"}],"author":"David Koziel","twitter_card":"summary_large_image","twitter_misc":{"Written by":"David Koziel","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/#article","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/"},"author":{"name":"David Koziel","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20"},"headline":"k6 Error Handling","datePublished":"2024-02-29T10:55:37+00:00","dateModified":"2024-02-29T10:55:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/"},"wordCount":326,"publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"keywords":["Cloud Load Testing","Error Handling","k6","k6 extensions","k6 load tests","Load Testing","logging","RedLine13"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/","url":"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/","name":"k6 Error Handling - RedLine13","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/#website"},"datePublished":"2024-02-29T10:55:37+00:00","dateModified":"2024-02-29T10:55:37+00:00","description":"In this brief article, we will detail how to intercept and log errors in your k6 load tests, as well as retrieve them for later review.","breadcrumb":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/k6-error-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.redline13.com\/blog\/"},{"@type":"ListItem","position":2,"name":"k6 Error Handling"}]},{"@type":"WebSite","@id":"https:\/\/www.redline13.com\/blog\/#website","url":"https:\/\/www.redline13.com\/blog\/","name":"RedLine13","description":"(Almost) Free Load Testing in the Cloud","publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.redline13.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.redline13.com\/blog\/#organization","name":"RedLine13","url":"https:\/\/www.redline13.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","contentUrl":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","width":300,"height":68,"caption":"RedLine13"},"image":{"@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20","name":"David Koziel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g","caption":"David Koziel"},"url":"https:\/\/www.redline13.com\/blog\/author\/dkoziel\/"}]}},"_links":{"self":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/comments?post=11073"}],"version-history":[{"count":3,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11073\/revisions"}],"predecessor-version":[{"id":11077,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11073\/revisions\/11077"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media\/11074"}],"wp:attachment":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media?parent=11073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/categories?post=11073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/tags?post=11073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}