[{"data":1,"prerenderedAt":1107},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Ferror-handling":3},{"id":4,"title":5,"body":6,"description":769,"extension":1097,"frontmatter":1098,"heading":5,"meta":1099,"navigation":1100,"order":1101,"path":1102,"seo":1103,"sitemap":1104,"source":4,"stem":1105,"__hash__":1106},"extensions\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Ferror-handling.md","Handle errors",{"type":7,"value":8,"toc":1089},"minimark",[9,22,28,264,274,277,426,444,449,468,617,621,631,755,761,765,784,896,902,906,919,1056,1068],[10,11,12,13,17,18,21],"p",{},"Handle failures from the client Kubb generates. Catch a ",[14,15,16],"code",{},"ResponseError",", switch to a returned error result with ",[14,19,20],{},"throwOnError",", and tell a network failure apart from a 4xx or 5xx response.",[10,23,24,25,27],{},"The generated client throws a ",[14,26,16],{}," on non-2xx responses by default:",[29,30,34],"pre",{"language":31,"code":32,"class":33},"typescript","import { getPetById } from '.\u002Fgen\u002Fclients\u002FgetPetById'\nimport { ResponseError } from '.\u002Fgen\u002F.kubb\u002Fclient'\n\ntry {\n  const { data } = await getPetById({ path: { petId: 1 } })\n  console.info(data.name)\n} catch (error) {\n  if (ResponseError.is(error)) {\n    console.error(error.status) \u002F\u002F 404\n    console.error(error.data) \u002F\u002F the parsed error body\n  }\n}","highlights",[14,35,37,64,65,64,82,64,84,64,92,64,138,64,165,64,180,64,203,64,230,64,253,64,260],{"class":36},"language-typescript",[38,39,42,46,50,54,57,60],"span",{"class":40,"style":41},"line","display: inline",[38,43,45],{"style":44},"color:#d73a49;--hls-dark:#f97583","import ",[38,47,49],{"style":48},"color:#005cc5;--hls-dark:#79b8ff","{ ",[38,51,53],{"style":52},"color:#24292e;--hls-dark:#e1e4e8","getPetById ",[38,55,56],{"style":48},"} ",[38,58,59],{"style":44},"from ",[38,61,63],{"style":62},"color:#032f62;--hls-dark:#9ecbff","'.\u002Fgen\u002Fclients\u002FgetPetById'","\n",[38,66,67,69,71,75,77,79],{"class":40,"style":41},[38,68,45],{"style":44},[38,70,49],{"style":48},[38,72,74],{"style":73},"color:#6f42c1;--hls-dark:#b392f0","ResponseError ",[38,76,56],{"style":48},[38,78,59],{"style":44},[38,80,81],{"style":62},"'.\u002Fgen\u002F.kubb\u002Fclient'",[38,83],{"class":40,"style":41},[38,85,86,89],{"class":40,"style":41},[38,87,88],{"style":44},"try ",[38,90,91],{"style":48},"{",[38,93,94,97,100,102,105,107,110,113,116,119,122,125,127,130,132,135],{"class":40,"style":41},[38,95,96],{"style":48},"  ",[38,98,99],{"style":44},"const ",[38,101,49],{"style":48},[38,103,104],{"style":52},"data ",[38,106,56],{"style":48},[38,108,109],{"style":44},"= ",[38,111,112],{"style":44},"await ",[38,114,115],{"style":73},"getPetById",[38,117,118],{"style":48},"({ ",[38,120,121],{"style":48},"path",[38,123,124],{"style":44},": ",[38,126,49],{"style":48},[38,128,129],{"style":48},"petId",[38,131,124],{"style":44},[38,133,134],{"style":48},"1 ",[38,136,137],{"style":48},"} })",[38,139,140,142,145,148,151,154,157,159,162],{"class":40,"style":41},[38,141,96],{"style":48},[38,143,144],{"style":52},"console",[38,146,147],{"style":44},".",[38,149,150],{"style":73},"info",[38,152,153],{"style":48},"(",[38,155,156],{"style":52},"data",[38,158,147],{"style":44},[38,160,161],{"style":48},"name",[38,163,164],{"style":48},")",[38,166,167,169,172,174,177],{"class":40,"style":41},[38,168,56],{"style":48},[38,170,171],{"style":44},"catch ",[38,173,153],{"style":48},[38,175,176],{"style":52},"error",[38,178,179],{"style":48},") {",[38,181,182,184,187,189,191,193,196,198,200],{"class":40,"style":41},[38,183,96],{"style":48},[38,185,186],{"style":44},"if ",[38,188,153],{"style":48},[38,190,16],{"style":73},[38,192,147],{"style":44},[38,194,195],{"style":73},"is",[38,197,153],{"style":48},[38,199,176],{"style":52},[38,201,202],{"style":48},")) {",[38,204,205,208,210,212,214,216,218,220,223,226],{"class":40,"style":41},[38,206,207],{"style":48},"    ",[38,209,144],{"style":52},[38,211,147],{"style":44},[38,213,176],{"style":73},[38,215,153],{"style":48},[38,217,176],{"style":52},[38,219,147],{"style":44},[38,221,222],{"style":48},"status",[38,224,225],{"style":48},") ",[38,227,229],{"style":228},"color:#6a737d;--hls-dark:#6a737d","\u002F\u002F 404",[38,231,232,234,236,238,240,242,244,246,248,250],{"class":40,"style":41},[38,233,207],{"style":228},[38,235,144],{"style":52},[38,237,147],{"style":44},[38,239,176],{"style":73},[38,241,153],{"style":48},[38,243,176],{"style":52},[38,245,147],{"style":44},[38,247,156],{"style":48},[38,249,225],{"style":48},[38,251,252],{"style":228},"\u002F\u002F the parsed error body",[38,254,255,257],{"class":40,"style":41},[38,256,96],{"style":228},[38,258,259],{"style":48},"}",[38,261,262],{"class":40,"style":41},[38,263,259],{"style":48},[10,265,266,267,270,271,147],{},"The ",[14,268,269],{},"message"," holds the HTTP method and the URL without its query string, so query credentials stay out of logs. For example: ",[14,272,273],{},"GET https:\u002F\u002Fapi.example.com\u002Fpets\u002F1 failed with status 404 Not Found",[10,275,276],{},"The error exposes these fields:",[29,278,280],{"language":31,"code":279,"class":33},"class ResponseError extends Error {\n  static is(error: unknown): error is ResponseError\u003Cunknown, unknown, unknown>\n  data: TError \u002F\u002F the parsed error body\n  status: number\n  statusText: string\n  contentType: string | undefined\n  request: AxiosRequestConfig\n  response: AxiosResponse\n}",[14,281,282,64,297,64,344,64,357,64,368,64,380,64,398,64,410,64,422],{"class":36},[38,283,284,287,289,292,295],{"class":40,"style":41},[38,285,286],{"style":44},"class ",[38,288,74],{"style":73},[38,290,291],{"style":44},"extends ",[38,293,294],{"style":73},"Error ",[38,296,91],{"style":48},[38,298,299,301,304,306,308,310,312,315,317,319,322,325,327,330,332,335,337,339,341],{"class":40,"style":41},[38,300,96],{"style":48},[38,302,303],{"style":44},"static ",[38,305,195],{"style":73},[38,307,153],{"style":48},[38,309,176],{"style":52},[38,311,124],{"style":52},[38,313,314],{"style":73},"unknown",[38,316,164],{"style":48},[38,318,124],{"style":44},[38,320,321],{"style":52},"error ",[38,323,324],{"style":44},"is ",[38,326,16],{"style":73},[38,328,329],{"style":44},"\u003C",[38,331,314],{"style":52},[38,333,334],{"style":44},", ",[38,336,314],{"style":52},[38,338,334],{"style":44},[38,340,314],{"style":52},[38,342,343],{"style":44},">",[38,345,346,348,350,352,355],{"class":40,"style":41},[38,347,96],{"style":44},[38,349,156],{"style":52},[38,351,124],{"style":52},[38,353,354],{"style":73},"TError ",[38,356,252],{"style":228},[38,358,359,361,363,365],{"class":40,"style":41},[38,360,96],{"style":228},[38,362,222],{"style":52},[38,364,124],{"style":52},[38,366,367],{"style":73},"number",[38,369,370,372,375,377],{"class":40,"style":41},[38,371,96],{"style":73},[38,373,374],{"style":52},"statusText",[38,376,124],{"style":52},[38,378,379],{"style":73},"string",[38,381,382,384,387,389,392,395],{"class":40,"style":41},[38,383,96],{"style":73},[38,385,386],{"style":52},"contentType",[38,388,124],{"style":52},[38,390,391],{"style":73},"string ",[38,393,394],{"style":44},"| ",[38,396,397],{"style":48},"undefined",[38,399,400,402,405,407],{"class":40,"style":41},[38,401,96],{"style":48},[38,403,404],{"style":52},"request",[38,406,124],{"style":52},[38,408,409],{"style":73},"AxiosRequestConfig",[38,411,412,414,417,419],{"class":40,"style":41},[38,413,96],{"style":73},[38,415,416],{"style":52},"response",[38,418,124],{"style":52},[38,420,421],{"style":73},"AxiosResponse",[38,423,424],{"class":40,"style":41},[38,425,259],{"style":48},[10,427,428,429,431,432,435,436,439,440,443],{},"Each generated package bundles its own ",[14,430,16],{}," class, so narrow with ",[14,433,434],{},"ResponseError.is(error)"," instead of ",[14,437,438],{},"instanceof",". ",[14,441,442],{},"is()"," checks the error name.",[445,446,448],"h2",{"id":447},"return-the-error-instead","Return the error instead",[10,450,451,452,455,456,458,459,461,462,464,465,467],{},"Pass ",[14,453,454],{},"throwOnError: false"," and the call resolves for every documented status. The result is a\ndiscriminated union: ",[14,457,156],{}," is set on success and ",[14,460,176],{}," on failure. Branch on ",[14,463,222],{},",\nbecause an empty error body can leave ",[14,466,176],{}," empty or undefined.",[29,469,471],{"language":31,"code":470,"class":33},"const result = await getPetById({ path: { petId: 1 }, throwOnError: false })\n\nif (result.status === 200) {\n  console.info(result.data.name)\n}\nif (result.status !== 200) {\n  console.error(result.status, result.error)\n}",[14,472,473,64,514,64,516,64,538,64,562,64,566,64,585,64,613],{"class":36},[38,474,475,477,480,482,484,486,488,490,492,494,496,498,500,502,504,506,508,511],{"class":40,"style":41},[38,476,99],{"style":44},[38,478,479],{"style":52},"result ",[38,481,109],{"style":44},[38,483,112],{"style":44},[38,485,115],{"style":73},[38,487,118],{"style":48},[38,489,121],{"style":48},[38,491,124],{"style":44},[38,493,49],{"style":48},[38,495,129],{"style":48},[38,497,124],{"style":44},[38,499,134],{"style":48},[38,501,259],{"style":48},[38,503,334],{"style":44},[38,505,20],{"style":48},[38,507,124],{"style":44},[38,509,510],{"style":48},"false ",[38,512,513],{"style":48},"})",[38,515],{"class":40,"style":41},[38,517,518,520,522,525,527,530,533,536],{"class":40,"style":41},[38,519,186],{"style":44},[38,521,153],{"style":48},[38,523,524],{"style":52},"result",[38,526,147],{"style":44},[38,528,529],{"style":48},"status ",[38,531,532],{"style":44},"=== ",[38,534,535],{"style":48},"200",[38,537,179],{"style":48},[38,539,540,542,544,546,548,550,552,554,556,558,560],{"class":40,"style":41},[38,541,96],{"style":48},[38,543,144],{"style":52},[38,545,147],{"style":44},[38,547,150],{"style":73},[38,549,153],{"style":48},[38,551,524],{"style":52},[38,553,147],{"style":44},[38,555,156],{"style":48},[38,557,147],{"style":44},[38,559,161],{"style":48},[38,561,164],{"style":48},[38,563,564],{"class":40,"style":41},[38,565,259],{"style":48},[38,567,568,570,572,574,576,578,581,583],{"class":40,"style":41},[38,569,186],{"style":44},[38,571,153],{"style":48},[38,573,524],{"style":52},[38,575,147],{"style":44},[38,577,529],{"style":48},[38,579,580],{"style":44},"!== ",[38,582,535],{"style":48},[38,584,179],{"style":48},[38,586,587,589,591,593,595,597,599,601,603,605,607,609,611],{"class":40,"style":41},[38,588,96],{"style":48},[38,590,144],{"style":52},[38,592,147],{"style":44},[38,594,176],{"style":73},[38,596,153],{"style":48},[38,598,524],{"style":52},[38,600,147],{"style":44},[38,602,222],{"style":48},[38,604,334],{"style":44},[38,606,524],{"style":52},[38,608,147],{"style":44},[38,610,176],{"style":48},[38,612,164],{"style":48},[38,614,615],{"class":40,"style":41},[38,616,259],{"style":48},[445,618,620],{"id":619},"set-the-generated-default","Set the generated default",[10,622,623,624,627,628,630],{},"Set ",[14,625,626],{},"throwOnErrorDefault"," on the client plugin to choose how generated operations handle non-2xx\nresponses by default. Each call can still override that setting with ",[14,629,20],{},":",[29,632,634],{"language":31,"code":633,"class":33},"import { pluginAxios } from '@kubb\u002Fplugin-axios'\n\npluginAxios({ throwOnErrorDefault: false })\n\n\u002F\u002F resolves with an error result\nconst list = await searchPets({ query: { status: 'available' } })\n\n\u002F\u002F opt one call back into throwing\nconst pet = await getPetById({ path: { petId: 1 }, throwOnError: true })",[14,635,636,64,652,64,654,64,669,64,671,64,676,64,708,64,710,64,715],{"class":36},[38,637,638,640,642,645,647,649],{"class":40,"style":41},[38,639,45],{"style":44},[38,641,49],{"style":48},[38,643,644],{"style":52},"pluginAxios ",[38,646,56],{"style":48},[38,648,59],{"style":44},[38,650,651],{"style":62},"'@kubb\u002Fplugin-axios'",[38,653],{"class":40,"style":41},[38,655,656,659,661,663,665,667],{"class":40,"style":41},[38,657,658],{"style":73},"pluginAxios",[38,660,118],{"style":48},[38,662,626],{"style":48},[38,664,124],{"style":44},[38,666,510],{"style":48},[38,668,513],{"style":48},[38,670],{"class":40,"style":41},[38,672,673],{"class":40,"style":41},[38,674,675],{"style":228},"\u002F\u002F resolves with an error result",[38,677,678,680,683,685,687,690,692,695,697,699,701,703,706],{"class":40,"style":41},[38,679,99],{"style":44},[38,681,682],{"style":52},"list ",[38,684,109],{"style":44},[38,686,112],{"style":44},[38,688,689],{"style":73},"searchPets",[38,691,118],{"style":48},[38,693,694],{"style":48},"query",[38,696,124],{"style":44},[38,698,49],{"style":48},[38,700,222],{"style":48},[38,702,124],{"style":44},[38,704,705],{"style":62},"'available' ",[38,707,137],{"style":48},[38,709],{"class":40,"style":41},[38,711,712],{"class":40,"style":41},[38,713,714],{"style":228},"\u002F\u002F opt one call back into throwing",[38,716,717,719,722,724,726,728,730,732,734,736,738,740,742,744,746,748,750,753],{"class":40,"style":41},[38,718,99],{"style":44},[38,720,721],{"style":52},"pet ",[38,723,109],{"style":44},[38,725,112],{"style":44},[38,727,115],{"style":73},[38,729,118],{"style":48},[38,731,121],{"style":48},[38,733,124],{"style":44},[38,735,49],{"style":48},[38,737,129],{"style":48},[38,739,124],{"style":44},[38,741,134],{"style":48},[38,743,259],{"style":48},[38,745,334],{"style":44},[38,747,20],{"style":48},[38,749,124],{"style":44},[38,751,752],{"style":48},"true ",[38,754,513],{"style":48},[10,756,757,760],{},[14,758,759],{},"client.setConfig({ throwOnError })"," does not change this default, because each generated\noperation passes its own value. Set it on the plugin.",[445,762,764],{"id":763},"network-failures-still-throw","Network failures still throw",[766,767,769,770,772,773,776,777,779,780,783],"blockquote",{"as":768},"important","",[14,771,20],{}," only applies when the server answers. A dropped connection, a DNS failure, or an\naborted ",[14,774,775],{},"AbortSignal"," always rejects. A ",[14,778,16],{}," means the server answered with a non-2xx\nstatus, and a ",[14,781,782],{},"ParseError"," means a body failed validation. Any other error usually means the\nrequest never completed.",[29,785,787],{"language":31,"code":786,"class":33},"try {\n  const { data } = await getPetById({ path: { petId: 1 }, throwOnError: false })\n  use(data)\n} catch (error) {\n  \u002F\u002F not a ResponseError: the request never completed\n  console.error('request failed to send', error)\n}",[14,788,789,64,795,64,839,64,852,64,864,64,871,64,892],{"class":36},[38,790,791,793],{"class":40,"style":41},[38,792,88],{"style":44},[38,794,91],{"style":48},[38,796,797,799,801,803,805,807,809,811,813,815,817,819,821,823,825,827,829,831,833,835,837],{"class":40,"style":41},[38,798,96],{"style":48},[38,800,99],{"style":44},[38,802,49],{"style":48},[38,804,104],{"style":52},[38,806,56],{"style":48},[38,808,109],{"style":44},[38,810,112],{"style":44},[38,812,115],{"style":73},[38,814,118],{"style":48},[38,816,121],{"style":48},[38,818,124],{"style":44},[38,820,49],{"style":48},[38,822,129],{"style":48},[38,824,124],{"style":44},[38,826,134],{"style":48},[38,828,259],{"style":48},[38,830,334],{"style":44},[38,832,20],{"style":48},[38,834,124],{"style":44},[38,836,510],{"style":48},[38,838,513],{"style":48},[38,840,841,843,846,848,850],{"class":40,"style":41},[38,842,96],{"style":48},[38,844,845],{"style":73},"use",[38,847,153],{"style":48},[38,849,156],{"style":52},[38,851,164],{"style":48},[38,853,854,856,858,860,862],{"class":40,"style":41},[38,855,56],{"style":48},[38,857,171],{"style":44},[38,859,153],{"style":48},[38,861,176],{"style":52},[38,863,179],{"style":48},[38,865,866,868],{"class":40,"style":41},[38,867,96],{"style":48},[38,869,870],{"style":228},"\u002F\u002F not a ResponseError: the request never completed",[38,872,873,875,877,879,881,883,886,888,890],{"class":40,"style":41},[38,874,96],{"style":228},[38,876,144],{"style":52},[38,878,147],{"style":44},[38,880,176],{"style":73},[38,882,153],{"style":48},[38,884,885],{"style":62},"'request failed to send'",[38,887,334],{"style":44},[38,889,176],{"style":52},[38,891,164],{"style":48},[38,893,894],{"class":40,"style":41},[38,895,259],{"style":48},[10,897,898,899,901],{},"Pass an ",[14,900,775],{}," to cancel a request. The call rejects with the abort reason.",[445,903,905],{"id":904},"validation-failures","Validation failures",[10,907,908,909,912,913,915,916,630],{},"When you turn on the ",[14,910,911],{},"validator"," option, a body that does not match its Zod schema throws a\n",[14,914,782],{}," instead of returning. It carries the schema's raw ",[14,917,918],{},"issues",[29,920,922],{"language":31,"code":921,"class":33},"import { ParseError } from '.\u002Fgen\u002F.kubb\u002FstandardSchema'\n\ntry {\n  const { data } = await getPetById({ path: { petId: 1 } })\n  use(data)\n} catch (error) {\n  if (error instanceof ParseError) {\n    console.error(error.issues) \u002F\u002F [{ message, path }]\n  }\n}",[14,923,924,64,940,64,942,64,948,64,982,64,994,64,1006,64,1023,64,1046,64,1052],{"class":36},[38,925,926,928,930,933,935,937],{"class":40,"style":41},[38,927,45],{"style":44},[38,929,49],{"style":48},[38,931,932],{"style":73},"ParseError ",[38,934,56],{"style":48},[38,936,59],{"style":44},[38,938,939],{"style":62},"'.\u002Fgen\u002F.kubb\u002FstandardSchema'",[38,941],{"class":40,"style":41},[38,943,944,946],{"class":40,"style":41},[38,945,88],{"style":44},[38,947,91],{"style":48},[38,949,950,952,954,956,958,960,962,964,966,968,970,972,974,976,978,980],{"class":40,"style":41},[38,951,96],{"style":48},[38,953,99],{"style":44},[38,955,49],{"style":48},[38,957,104],{"style":52},[38,959,56],{"style":48},[38,961,109],{"style":44},[38,963,112],{"style":44},[38,965,115],{"style":73},[38,967,118],{"style":48},[38,969,121],{"style":48},[38,971,124],{"style":44},[38,973,49],{"style":48},[38,975,129],{"style":48},[38,977,124],{"style":44},[38,979,134],{"style":48},[38,981,137],{"style":48},[38,983,984,986,988,990,992],{"class":40,"style":41},[38,985,96],{"style":48},[38,987,845],{"style":73},[38,989,153],{"style":48},[38,991,156],{"style":52},[38,993,164],{"style":48},[38,995,996,998,1000,1002,1004],{"class":40,"style":41},[38,997,56],{"style":48},[38,999,171],{"style":44},[38,1001,153],{"style":48},[38,1003,176],{"style":52},[38,1005,179],{"style":48},[38,1007,1008,1010,1012,1014,1016,1019,1021],{"class":40,"style":41},[38,1009,96],{"style":48},[38,1011,186],{"style":44},[38,1013,153],{"style":48},[38,1015,321],{"style":52},[38,1017,1018],{"style":44},"instanceof ",[38,1020,782],{"style":73},[38,1022,179],{"style":48},[38,1024,1025,1027,1029,1031,1033,1035,1037,1039,1041,1043],{"class":40,"style":41},[38,1026,207],{"style":48},[38,1028,144],{"style":52},[38,1030,147],{"style":44},[38,1032,176],{"style":73},[38,1034,153],{"style":48},[38,1036,176],{"style":52},[38,1038,147],{"style":44},[38,1040,918],{"style":48},[38,1042,225],{"style":48},[38,1044,1045],{"style":228},"\u002F\u002F [{ message, path }]",[38,1047,1048,1050],{"class":40,"style":41},[38,1049,96],{"style":228},[38,1051,259],{"style":48},[38,1053,1054],{"class":40,"style":41},[38,1055,259],{"style":48},[10,1057,1058,1059,1061,1062,1064,1065,1067],{},"A ",[14,1060,782],{}," reports schema validation issues. A ",[14,1063,16],{}," reports a non-2xx status. With ",[14,1066,454],{},", the error body is checked against its own error schema.",[10,1069,1070,1071,1074,1075,1077,1078,1080,1081,1084,1085,1088],{},"Calling ",[14,1072,1073],{},".unwrap()"," on a ",[14,1076,454],{}," call turns that same ",[14,1079,176],{}," into a rejection, so a\n",[14,1082,1083],{},"try","\u002F",[14,1086,1087],{},"catch"," works there too.",{"title":769,"searchDepth":1090,"depth":1090,"links":1091},5,[1092,1094,1095,1096],{"id":447,"depth":1093,"text":448},2,{"id":619,"depth":1093,"text":620},{"id":763,"depth":1093,"text":764},{"id":904,"depth":1093,"text":905},"md",{},{},true,null,"\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Ferror-handling",{"title":5},{"loc":1102},"extensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Ferror-handling","T029o27SekkgU10XGy_8rFB-3b2R0dlOIjh-wJxcFak",1792859161296]